javascript - 在 JavaScript 函数中检索“confirm()”值时出现 Asp.net 问题
问题描述
我想知道为什么 Java Script 函数没有正确返回 Confirm 值。
我有这个按钮:
<asp:Button ID="btnSaveAgreement" runat="server" UseSubmitBehavior="false" Text="Save" OnClientClick ="ConfirmSaveWithoutSendEmail(this);" />
我有这个 Java 脚本功能:
function ConfirmSaveWithoutSendEmail(btnToDisableWhileSubmitting)
{
var IsPanelToSendEmailVisible = document.getElementById('<%= hfIsSendEmailPanelVisible.ClientID%>').value;
if (IsPanelToSendEmailVisible > 0)
{
//This section takes a confirmation "Ok/Cancel" to submit or not
//the data, the problem is that if I choose "Cancel", the code
//on the button is executed, the behavior I would except is such
//that selecting "Cancel" the code on my button is not
//executed.
return confirm("Do you want to save without send email?");
}
else
{
//this section disable the button while submitting information,
//this section works fine.
if (!Page_ClientValidate())
return false;
btnToDisableWhileSubmitting.disabled = true;
btnToDisableWhileSubmitting.value = 'Saving...';
return true;
}
}
为什么选择的确认值总是被认为是真的?
我想知道我错过了什么。
解决方案
问题是因为您在OnClientClick
属性中设置的代码实际上并没有返回任何内容,它只是执行ConfirmSaveWithoutSendEmail
函数但实际上并未使用它的返回值。您还应该进行设置UseSubmitBehavior="true"
,以便它在点击时提交您的表单。尝试这个:
<asp:Button ID="btnSaveAgreement" runat="server" UseSubmitBehavior="true" Text="Save" OnClientClick="return ConfirmSaveWithoutSendEmail(this);" />
else
您的声明部分也if
将不起作用,因为您在它能够提交表单之前禁用了该按钮。如果出现以下表单,您将需要禁用该submit
表单:
document.getElementById('yourFormId')
.addEventListener('submit', function() {
var submitBtn = document.getElementById('<%= btnSaveAgreement.ClientID %>');
submitBtn.disabled = true;
submitBtn.value = 'Saving...';
});
推荐阅读
- rpm - 安装 rpm 包时替换现有目录及其所有内容
- java - 全新安装的 Ubuntu 16.04 LTS 服务器和升级的服务器之间的编码问题
- php - 网络服务器上的 NumberFormatter 输出错误,而不是另一个
- python - 如何在不使用 refresh_from_db 的情况下强制 Django 模型在保存后重新加载字段?
- eclipse - 如何从 Eclipse 中自动编译和运行 xtext mydsl 应用程序?
- java - 如何测试异步方法
- javascript - 将 $.each 和 $.getJSON 转换为 vanilla javascript
- javascript - 多个 Javascript 文件不起作用
- c++ - 模板函数重载。理解字符
- xml - 使用 XSL 获取第二次出现的值