按钮点击javascript中的确认框,如果返回true,则执行相同按钮的点击事件

时间:2014-05-12 11:26:04

标签: javascript asp.net

我有一个asp.net按钮控件和一个javascript确认框,在该按钮点击执行。如果javascript返回true,则只执行按钮的单击事件..

HTML:

    <asp:TextBox ID="txtSubject" runat="server" CssClass="textbox" Width="454px" CausesValidation="true" onfocus="ddlSelect()"></asp:TextBox>


    <asp:Button ID="btnSaveSend" CssClass="myButton" runat="server" Text="Save & Send"
                                        OnClientClick="javascript:return SubjectEmpty()" OnClick="btnSaveSend_Click" /> 

使用Javascript:

    function SubjectEmpty() {
    var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
    var result = confirm("Are you sure you want to send mail:" + subject + " ? We shell        check for the availability..");
        if (result == true) {
            return true;
        }
        else {
            return false;
        }

代码背后:

    protected void btnSaveSend_Click(object sender, EventArgs e)
    {
        //Do something
    }

问题是即使我选择取消它执行btnSaveSend_click()

4 个答案:

答案 0 :(得分:1)

  function SubjectEmpty() {
         var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
         if (confirm("Are you sure you want to send mail:" + subject + " ? We shell                    
         check for the availability..")) 
         {
             return true;
         }
         else
         {
             return false;
         }
  }

答案 1 :(得分:1)

试试这个..你应该添加一个return false;来阻止服务器事件被触发..

function SubjectEmpty() {
    var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
    var result = confirm("Are you sure you want to send mail:" + subject + " ? We shall  check for the availability..");
     if (result == true) {
         return true;
     }
      else
     {
         return false;
     }
 }

答案 2 :(得分:0)

更改您的Javascript函数,如下所示。它会对你有所帮助。

function SubjectEmpty() {
    var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
    var result = confirm("Are you sure you want to send mail:" + subject + " ? We shell        check for the availability..");
     if (result == true) {
         return true;
     }
     return false;
 }

答案 3 :(得分:0)

对不起,我的错误..

我在页面中使用了ajax控件。所以即使我点击取消也正在执行点击事件。我之前不知道这是因为ajax。所以我还有其他出路:

    if (!confirm("Are you sure you want to send mail:" + subject + " of type "+type+" on " + sendDate + " at " + hour + ":" + min + ":00.. We shell check for the availability..")) {
            document.getElementById('<%=btnSaveSend.ClientID%>').disabled = true;                
        }