最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - POST after disabling button on Google Chrome doesn't work - Stack Overflow

programmeradmin2浏览0评论

I create ASP .NET MVC application. On button click I call JavaScript that disable same button. After that (button has property readonly disabled) post action doesn't call. When I remove JavaScript that disable buttons everything is fine. This happens only in the Google Chrome.

When I try same example in Firefox or Internet Explorer everything is fine.

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

I create ASP .NET MVC application. On button click I call JavaScript that disable same button. After that (button has property readonly disabled) post action doesn't call. When I remove JavaScript that disable buttons everything is fine. This happens only in the Google Chrome.

When I try same example in Firefox or Internet Explorer everything is fine.

<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>
Share Improve this question edited Nov 16, 2011 at 20:13 Patricia 7,8024 gold badges39 silver badges70 bronze badges asked Nov 16, 2011 at 20:05 SilverDeveloperSilverDeveloper 1872 silver badges13 bronze badges 4
  • You should use $('...').prop('disabled', false); for those attributes (source) – Didier Ghys Commented Nov 16, 2011 at 20:07
  • 1 What is it you're trying to do? Prevent the form being submitted twice? – nachito Commented Nov 16, 2011 at 20:09
  • 2 @DidierG. OP is using jquery 1.4.4, prop is available in 1.6 or higher i think – Rafay Commented Nov 16, 2011 at 20:21
  • @nachito - Yes, I try prevent form being submitted twice. I also try include jQuery 1.7 and try with $('...').prop('disabled', false) but doesn't help. – SilverDeveloper Commented Nov 16, 2011 at 20:39
Add a ment  | 

2 Answers 2

Reset to default 5

You need to call form.submit() before disabling the button, and return false from your event handler.

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

<html xmlns="http://www.w3/1999/xhtml">



<head>

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

<title>Untitled 1</title>

</head>



<body>

<script src="//code.jquery./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();
}

edit

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

});
发布评论

评论列表(0)

  1. 暂无评论