确认消息框返回错误值

时间:2022-03-21 10:25:13

I have a script for a confirm pop up window.When I call this in our aspx.cs page it returns different values.My script is

我有一个确认弹出窗口的脚本。当我在aspx.cs页面中调用它时,它返回不同的值。我的脚本是

<script type="text/javascript">
function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("Do you want to remove this employee from this group?")) {
        confirm_value.value = "1";
    } else {
        confirm_value.value = "2";
    }
    document.forms[0].appendChild(confirm_value);
}

Button

<asp:Button ID="btnAdd"  OnClientClick = "Confirm()" runat="server"    ValidationGroup="1" onclick="btnAdd_Click" />

Button click function

按钮单击功能

 protected void btnAdd_Click(object sender, EventArgs e)
    {
   string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "1")
        {
          //Do something
        }
      else
       {
    //Do something
       }

    }

First time I clicked cancel then my confirmvalue=="1" and next time again I selected ok then my confirmvalue=="1,2"in place of 2. How it returns error value.

我第一次点击取消然后我的确认值==“1”然后我再次选择确定然后我的confirmvalue ==“1,2”代替2.它如何返回错误值。

2 个解决方案

#1


4  

You're creating multiple inputs named "confirm_value", one each time you're clicking the button. What you need to do is reuse the same input:

您正在创建名为“confirm_value”的多个输入,每次单击按钮时都会输入一个。您需要做的是重用相同的输入:

function Confirm() {
    var confirm_value = document.querySelector('[name="confirm_value"]');
    if (!confirm_value) {
        confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        document.forms[0].appendChild(confirm_value);
    }
    if (confirm("Do you want to remove this employee from this group?")) {
        confirm_value.value = "1";
    } else {
        confirm_value.value = "2";
    }    
}

#2


1  

function Confirm() {
var Result=confirm("Do you want to remove this employee from this group?");
var confirm_value = document.querySelector('[name="confirm_value"]');
 if (Result) {
return true;
 } else {
return false;
}    
}

#1


4  

You're creating multiple inputs named "confirm_value", one each time you're clicking the button. What you need to do is reuse the same input:

您正在创建名为“confirm_value”的多个输入,每次单击按钮时都会输入一个。您需要做的是重用相同的输入:

function Confirm() {
    var confirm_value = document.querySelector('[name="confirm_value"]');
    if (!confirm_value) {
        confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        document.forms[0].appendChild(confirm_value);
    }
    if (confirm("Do you want to remove this employee from this group?")) {
        confirm_value.value = "1";
    } else {
        confirm_value.value = "2";
    }    
}

#2


1  

function Confirm() {
var Result=confirm("Do you want to remove this employee from this group?");
var confirm_value = document.querySelector('[name="confirm_value"]');
 if (Result) {
return true;
 } else {
return false;
}    
}