确认框:单击取消按钮后,确定按钮不起作用

时间:2015-05-26 12:50:04

标签: javascript jquery html vb.net

我有一个带有确认框的javascript函数。这是在按钮点击时调用的。当我点击按钮时,出现确认框和确定和取消按钮。如果我先点击确定,那么它的工作正常。但是,当我单击取消然后重新填充确认框并单击确定然后它不起作用。

的JavaScript

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("Are you sure you want to cancel?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);
}

按钮

  <asp:Button ID="BtnCancelBooking" runat="server" Text="Cancel My Application" style="background-color:#5A105A;color:#ffffff" 
CssClass="FormsubmitButton" OnClientClick = "Confirm()"
  Width="140px" ValidationGroup="" Visible="True" />

VB代码:

 Protected Sub BtnCancelBooking_Click(sender As Object, e As System.EventArgs) Handles BtnCancelBooking.Click

    Dim confirmValue As String = Request.Form("confirm_value")
    If confirmValue = "Yes" Then


        Dim b As Boolean = objClsRoomBooking.CancelBooking(Convert.ToInt32(Session("AppID")), Convert.ToInt32(Session("StdYearID")), Convert.ToInt32(Session("ComID")))
        If b = True Then
            'ShowMessages(1, "Your Application has been Cancelled ")
            Response.Redirect("Details.aspx")
        Else
            ShowMessages(2, "Applicant Status has not been Cancelled")
        End If

    Else
          // else 
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

每次重新填充确认框时,您的代码都会向表单附加重复的隐藏元素,将您的功能更改为此,

    function Confirm() {
        var tmpResp = "";
        if (confirm("Are you sure you want to cancel?")) {
            tmpResp = "Yes";
        } else {
            tmpResp = "No";
        }
        if(document.getElementById("confirm_value")) {
           document.getElementById("confirm_value").value = tmpResp;
        }
        else {
           var confirm_value = document.createElement("INPUT");
           confirm_value.type = "hidden";
           confirm_value.name = "confirm_value";
           confirm_value.id = "confirm_value";
           confirm_value.value= tmpResp;
           document.forms[0].appendChild(confirm_value);
        }
    }

此代码检查hidden-field是否已退出,如果是,则为其分配confirm-box响应,否则创建新hidden-field并附加到form。 希望它有所帮助。