我正在开发一个Asp.Net应用程序,当前我正在尝试从后面的代码中触发JavaScript Confirm()
弹出窗口。我想弹出而不单击任何button_click
事件。
IF not blnResult then
popup message with OK& CANCEL
IF OK THEN Exit Sub
ELSE
no display
END IF
我试图做以下事情,但它不触发弹出窗口,请协助。
1)在ASPX中创建了一个按钮
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="Confirm()"/>
JavaScript函数
<script language="javascript" type = "text/javascript">
function Confirm() {
var confirm_value1 = document.createElement("INPUT");
confirm_value1.type = "hidden";
confirm_value1.name = "confirm_value";
if (confirm("Do you want to delete all records?")) {
confirm_value1.value = "Yes";
} else {
confirm_value1.value = "No";
}
document.forms[0].appendChild(confirm_value1);
}
</script>
代码后面,
Public function GetConfirmation()
btnConfirm_Click(btnConfirm, EventArgs.Empty)
End Sub
上一行没有为我触发弹出窗口。
答案 0 :(得分:0)
只需修改您的javascript函数。
<script language="javascript" type = "text/javascript">
function Confirm() {
var confirm_value1 = document.createElement("INPUT");
confirm_value1.type = "hidden";
confirm_value1.name = "confirm_value";
if (confirm("Do you want to delete all records?")) {
confirm_value1.value = "Yes";
} else {
confirm_value1.value = "No";
}
document.forms[0].appendChild(confirm_value1);
return false;//this will prevent submit click.
} </script>
只需在函数的最后一行添加return语句。
返回假;
我希望该解决方案能够为您提供帮助。
答案 1 :(得分:0)
如果我正确理解,那么可能只有在用户通过单击“是”进行确认时,您才想在单击事件后面调用代码。有很多方法可以做到这一点。我只列出了一些。选择最适合您的。
不知道为什么创建了隐藏的输入字段。创建它的目的是什么。如果您不需要隐藏的输入字段,可以尝试一下。
- HTML标记中的确认
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="return confirm('Do you want to delete all records?');"/>
- JavaScript确认
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="Confirm();"/>
<script language="javascript" type="text/javascript">
function Confirm() {
return confirm("Do you want to delete all records?");
}
</script>
或
<script language="javascript" type="text/javascript">
function Confirm() {
var result = confirm("Do you want to delete all records?");
return result;
}
</script>
如果您确实需要保留隐藏的输入字段,请在下面使用。
<script language="javascript" type="text/javascript">
function Confirm() {
var confirm_value1 = document.createElement("INPUT");
confirm_value1.type = "hidden";
confirm_value1.name = "confirm_value";
var result = confirm("Do you want to delete all records?");
confirm_value1.value = result ? "Yes" : "No";
document.forms[0].appendChild(confirm_value1);
return result;
}
</script>