确认框:点击取消按钮后,确定按钮不工作

Confirmation Box : OK button not working after clicking on Cancel Button

本文关键字:按钮 工作 确认 取消      更新时间:2023-09-26

我有一个javascript函数与确认框。它被称为按钮点击。当我点击按钮,确认框与确定和取消按钮出现。如果我先点击OK,那么它工作得很好。但是当我点击取消,然后重新填充确认框,然后点击确定,它就不起作用了。

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);
}
<<p> 按钮/strong>
  <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

每次重新填充确认框时,您的代码都会向表单添加重复的隐藏元素,请将函数更改为

    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