2

Javascript:返回 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"/>
4

3 回答 3

2

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

<asp:Button ID="btnUpdate" runat="server" 
        OnClick="btnUpdate_Click" Text="Close Request" 
         OnClientClick="if(TestCheckBox()) return UpdateConfirmation();"/>
于 2013-02-21T06:42:21.627 回答
1

您应该使用两个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是主按钮。
不要忘记设置UseSubmitBehaviorfalse

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


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

于 2013-02-21T06:37:35.893 回答
1

感谢 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"/>
于 2013-02-21T08:22:46.473 回答