2013-02-21 40 views
2

的Javascript:返回false不工作,即使我按取消代码被执行的Javascript:返回false不工作:即使我按取消代码被执行

我试图执行2倍的JavaScript与1个按钮。当第二个脚本执行时,即使我单击取消,代码也会执行。以下是代码。有人能帮帮我吗?

<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"/> 
+0

您从事件处理程序返回'B',不'UpdateConfirmation的返回值'。 – 2013-02-21 06:29:47

+0

@Aditya我在你的问题答案中加了一些解释。 – nnnn 2013-02-21 07:18:21

回答

1

感谢沙夫克和所有其他人也

工作代码

<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"/> 
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代码来点击按钮。

+0

这甚至不能解决问题。即使它做到了(通过从“OnClientClick”返回false,它不会),这是非常麻烦的。 – 2013-02-21 06:52:47

2

你应该在调用你的函数时添加返回值。

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