如何解决在JavaScript函数中检索“确认()”值时ASP.NET问题?/

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (44)

我想知道为什么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...';
    });

扫码关注云+社区

领取腾讯云代金券