POST后禁用按钮在谷歌浏览器不工作

POST after disabling button on Google Chrome doesn't work

本文关键字:谷歌浏览器 工作 按钮 POST      更新时间:2023-09-26

我创建了asp.net MVC应用程序。在按钮点击,我调用JavaScript禁用相同的按钮。之后(按钮有属性只读禁用)post action不调用。当我删除禁用按钮的JavaScript时,一切都很好。这只发生在Google Chrome中。

当我在Firefox或Internet Explorer中尝试相同的示例时,一切都很好。

<script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
    jQuery(document).ready(function () {
        Enable();
    });

    function Enable() {
        $('#btn').removeAttr('disabled');
        $('#btn').removeAttr('readonly');
    }
    function Disable() {
        $('#btn').attr('disabled', 'true');
        $('#btn').attr('readonly', 'true');
    }
</script>
<h2>Example</h2>
<form>
    <input id="btn" type="submit" value="StackOverflow" onclick="Disable()" />
</form>

您需要在禁用按钮之前调用form.submit(),并从事件处理程序返回false。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<script src="//code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script> 

<script type="text/javascript"> 

    jQuery(document).ready(function () { 
        Enable(); 
    }); 


    function Enable() { 
        $('#btn').removeAttr('disabled'); 
        $('#btn').removeAttr('readonly'); 
    } 

    function Disable() { 
        $('#myform').submit();

        $('#btn').attr('disabled', 'true'); 
        $('#btn').attr('readonly', 'true'); 

        return false;
    } 
</script> 
<h2>Example</h2> 

<form id="myform" method="post"> 
    <input id="btn" type="submit" value="StackOverflow" onclick="return Disable()" /> 
</form> 

</body>

</html>

try

function Disable() {
    $('#btn').attr('disabled', 'disabled');
    $('#btn').attr('readonly', 'readonly');
   $("form").submit();
}

编辑
change the `input` type to `button`
<form>
    <input id="btn" type="button" value="StackOverflow" />
</form>
$(function(e){
$("#btn").click(function(e){
e.preventDefault();
$(this).attr('disabled', 'disabled'); //if you can use latest version of jquery and use .prop("disabled",true)
$("form").submit();
});
});