2013-02-21 39 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();"/>