Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与asp.net webforms中的禁用按钮代码一起使用时,javascript确认警报不起作用_Javascript_Asp.net_Webforms - Fatal编程技术网

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

与asp.net webforms中的禁用按钮代码一起使用时,javascript确认警报不起作用,javascript,asp.net,webforms,Javascript,Asp.net,Webforms,我有一个网络表单,上面有一个像这样的提交按钮 <asp:Button ID="btnSub" runat="server" Text="submit" OnClientClick="return processSub();" /> function processSub(){ var btnId = '<%= btnSub.ClientID %>' var userSelection = confirm('Are you sure you wish

我有一个网络表单,上面有一个像这样的提交按钮

<asp:Button ID="btnSub" runat="server" Text="submit" OnClientClick="return processSub();" />
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;
    }
}

它调用的javascript函数如下

<asp:Button ID="btnSub" runat="server" Text="submit" OnClientClick="return processSub();" />
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;
    }
}
函数processSub(){
变量btnId=''
var userSelection=confirm('您确定要提交吗');
如果(用户选择){
document.getElementById(btnId).setAttribute(“禁用”、“禁用”);
返回true;
}否则{
返回false;
}
}
现在我想在用户单击submit时禁用submit按钮。但是当我调用代码将属性设置为disabled时,提交不会发生。 当我注释这一行时,
document.getElementById(btnId.setAttribute)(“disabled”,“disabled”)在IF块中,它工作并继续提交

为什么会这样?我如何让它工作?我没有使用任何JavaScript库,也不想使用


谢谢。

在返回if语句之前,通过js提交表单

document.forms[0].submit();

即使按钮上没有指定
OnClick
,但单击时仍会执行表单post(回发)。您可以使用javascript设置禁用状态,但在回发发生后立即将按钮重置为原始状态

如果要禁用该按钮,可以在用户确认后在code begind中进行设置

<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;
}

受保护的无效btnSub_单击(对象发送方,事件参数e)
{
//禁用按钮
btnSub.Enabled=false;
}

我不想使用代码隐藏。禁用客户端与我的代码一起工作,但当使用该代码禁用时,它不会执行提交。这就是问题所在。当我注释掉“禁用”时,会发生提交。这是其中之一。返回true后将有一个提交。您无法通过先禁用按钮来防止这种情况。或者在这两种情况下都返回false。这就是我要说的。在这一行
document.getElementById(btnId).setAttribute(“已禁用”、“已禁用”)之后,返回true后没有提交当我注释该行时,返回true有效,提交发生。是的,这就是为什么在回发后需要(再次)禁用它。在代码隐藏中或在页面重新加载后使用jquery。