【问题标题】:Javascript : Return false is not working : even after I press cancel the code gets executed [closed]Javascript:返回false不起作用:即使我按下取消,代码也会被执行[关闭]
【发布时间】:2013-02-21 06:20:09
【问题描述】:

Javascript : Return false 不起作用,即使在我按下取消后,代码也会被执行

我正在尝试使用 1 个按钮执行 2 个 javascript。当第二个脚本执行时,即使我单击取消,代码也会被执行。下面是代码。有人可以帮帮我吗?

<script type="text/javascript" language="javascript">
  var TargetBaseControl = null;
  window.onload = function () {
    try {
      //get target base control.
      TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID %>');
    } catch (err) {
      TargetBaseControl = null;
    }
  }

  function TestCheckBox() {
    if (TargetBaseControl == null) return false;
    //get target child control.
    var TargetChildControl = "chkSelect";
    //get all the control of the type INPUT in the base control.
    var Inputs = TargetBaseControl.getElementsByTagName("input");
    for (var n = 0; n < Inputs.length; ++n)
    if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 && Inputs[n].checked) return true;
    alert('Please select at least one Request to Close!');
    return false;
  }

  function UpdateConfirmation() {
    if (confirm("Are you sure, you want to close selected request ?") == true) return true;
    else return false;
  }
</script>




<asp:Button ID="btnUpdate" runat="server" 
        OnClick="btnUpdate_Click" Text="Close Request" 
         OnClientClick="var b = TestCheckBox(); if (b) UpdateConfirmation(); return b"/>

【问题讨论】:

  • 你从事件处理程序返回b,而不是UpdateConfirmation的返回值。
  • @Aditya 我在你的回答中添加了一些解释。

标签: javascript asp.net return


【解决方案1】:

您应该在调用函数时添加 return。

<asp:Button ID="btnUpdate" runat="server" 
        OnClick="btnUpdate_Click" Text="Close Request" 
         OnClientClick="if(TestCheckBox()) return UpdateConfirmation();"/>

【讨论】:

    【解决方案2】:

    您应该使用两个asp:Button(一个按钮是不可见的)。例如 -

    //some code
    //
    function UpdateConfirmation() {
        if (confirm("Are you sure, you want to close selected request ?") == true) 
        document.getElementById('<%= this.btn.ClientID %>').click();//fire hidden button
        else return false;
      }
    </script>
    
    
    <asp:Button ID="btnUpdate" runat="server" Text="Close Request" 
       OnClientClick="var b = TestCheckBox(); if (b) UpdateConfirmation(); return b" 
       UseSubmitBehavior="false"/>
    <asp:Button ID="btn" runat="server" Text="Button" 
        OnClick="btnUpdate_Click" CssClass="VisibleFalse" TabIndex="-1" />
    


    btn 是隐藏按钮,btnUpdate 是主按钮。
    不要忘记将UseSubmitBehavior 设置为false

    编辑:使用document.getElementById('&lt;%= this.btn.ClientID %&gt;').click(); 而不是document.getElementById(ClientIDPrefix + "btn").click();。


    它只是一个触发按钮点击的 javascript 代码。

    【讨论】:

    • 不过,这甚至不能解决问题。而且它更加混乱,即使它确实如此(通过从 OnClientClick 返回 false,它没有)。
    【解决方案3】:

    感谢 Shafeeq 和其他所有人

    工作代码

    <script type="text/javascript" language="javascript">
    
                 var TargetBaseControl = null;
    
                 window.onload = function () {
                     try {
                         //get target base control.
                         TargetBaseControl =
           document.getElementById('<%= this.GridView1.ClientID %>');
                     }
                     catch (err) {
                         TargetBaseControl = null;
                     }
                 }
    
                 function TestCheckBox() 
                 {
                     if (TargetBaseControl == null) return false;
    
                     //get target child control.
                     var TargetChildControl = "chkSelect";
    
                     //get all the control of the type INPUT in the base control.
                     var Inputs = TargetBaseControl.getElementsByTagName("input");
    
                     for (var n = 0; n < Inputs.length; ++n)
                         if (Inputs[n].type == 'checkbox' &&
            Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 &&
            Inputs[n].checked)
                             return true;
    
                     alert('Please select at least one Request to Close!');
                     return false;
                 }
    
    
              function UpdateConfirmation() {
                  if (confirm("Are you sure, you want to close selected request ?") == true)
    
    
                         return true;
    
                     else return false;
    
                 }
    

    <asp:Button ID="btnUpdate" runat="server" 
            Text="Close Request" 
            OnClick="btnUpdate_Click" 
    OnClientClick="var b = TestCheckBox(); if (b) return UpdateConfirmation(); return b"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2017-04-12
      相关资源
      最近更新 更多