简体   繁体   English

当与asp.net Webforms中的禁用按钮代码一起使用时,javascript确认警报不起作用

[英]javascript confirm alert not working when used with disable button code in asp.net webforms

I have a webform with a submit button like this 我有一个带有提交按钮的网络表单

<asp:Button ID="btnSub" runat="server" Text="submit" OnClientClick="return processSub();" />

The javascript function it calls is like this 它调用的javascript函数是这样的

function processSub(){

    var btnId = '<%= btnSub.ClientID %>'

    var userSelection = confirm('Are you sure you wish to submit');
    if (userSelection){
         document.getElementById(btnId).setAttribute("disabled","disabled");
         return true;
    } else {
         return false;
    }
}

Now I want to disable the submit button when the user clicks submit. 现在,我想在用户单击“提交”时禁用“提交”按钮。 But when I call the code to set the attribute to disabled the submit doesn't happen. 但是,当我调用将属性设置为禁用的代码时,提交不会发生。 When I comment this line document.getElementById(btnId).setAttribute("disabled","disabled"); 当我评论此行document.getElementById(btnId).setAttribute("disabled","disabled"); in the IF block, it works and continues with the submit. 在IF块中,它可以正常工作并继续提交。

Why is this so and how do I get it to work? 为什么会这样,如何使它正常工作? I am not using any JavaScript library and don't want to as well. 我没有使用任何JavaScript库,也不想这样做。

Thanks. 谢谢。

Even though there is no OnClick specified on the button, it will still do a form post (PostBack) when it is clicked. 即使在按钮上未指定OnClick ,单击后仍会执行表单发布(PostBack)。 You set the disabled state with javascript, but immediately after a PostBack occurs therefore resetting the Button to it's original state. 您使用javascript设置了disabled状态,但在发生PostBack后立即将其重置为原始状态。

if you want to disable the button you can set it in code begind after the user confirmes. 如果要禁用该按钮,则可以在用户确认后以开始的代码进行设置。

<asp:Button ID="btnSub" runat="server" Text="submit" OnClientClick="return processSub();" 
    OnClick="btnSub_Click" />

protected void btnSub_Click(object sender, EventArgs e)
{
    //disable the button
    btnSub.Enabled = false;
}

通过js提交表单,然后返回if语句

document.forms[0].submit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM