SilverDeveloper SilverDeveloper - 20 days ago 5
Javascript Question

POST after disabling button on Google Chrome doesn't work

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>

Answer

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