我有一个确认弹出窗口的脚本。当我在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);
}
按钮
<asp:Button ID="btnAdd" OnClientClick = "Confirm()" runat="server" ValidationGroup="1" onclick="btnAdd_Click" />
按钮点击功能
protected void btnAdd_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "1")
{
//Do something
}
else
{
//Do something
}
}
我第一次点击取消然后点击我的confirmvalue=="1"
,然后我再次选择确定,然后我的confirmvalue=="1,2"
代替2.如何返回错误值。
答案 0 :(得分:4)
您每次点击按钮时都会创建名为&#34; confirm_value&#34;的多个输入。您需要做的是重用相同的输入:
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";
}
}
答案 1 :(得分: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;
}
}