我想知道为什么Java脚本函数没有正确地返回确认值。
我有这个按钮:
<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;
}
}
为什么选择的确认值总是被认为是真?
我想知道我错过了什么。
发布于 2018-10-10 08:26:51
问题是,您在OnClientClick
属性中设置的代码实际上并没有返回任何东西,它只是执行ConfirmSaveWithoutSendEmail
函数,而不是实际使用它的返回值。您还应该设置UseSubmitBehavior="true"
,以便它在单击时提交表单。试试这个:
<asp:Button ID="btnSaveAgreement" runat="server" UseSubmitBehavior="true" Text="Save" OnClientClick="return ConfirmSaveWithoutSendEmail(this);" />
if
语句的else
部分也将不起作用,因为您在按钮提交表单之前禁用了该按钮。您需要在表单的submit
事件中禁用表单:
document.getElementById('yourFormId')
.addEventListener('submit', function() {
var submitBtn = document.getElementById('<%= btnSaveAgreement.ClientID %>');
submitBtn.disabled = true;
submitBtn.value = 'Saving...';
});
https://stackoverflow.com/questions/52730943
复制相似问题