开发者

Disable button after post using JS/Jquery

I would like to have a function to add an onclick event to my form buttons to get them disabled in order to avoid double posting.

<form>
   <button onclick="disable(this)" type="submit">post</butto开发者_如何学编程n>
</form>
<script> function disable(button){ $(button). ??? }</script>

Ani idea or tip? thanks!


The button must be an input element (if you don't submit the form via JavaScript). A <button> element has no attribute type and setting it will have no effect.
I also suggest to attach the click handler via jQuery:

<form>
    <!-- ... -->
    <input id="submit" type="submit" value="post" />
</form>
<script type="text/javascript>
    $('#submit').click(function() {
        $(this).attr('disabled', true);
    });
</script>

Reference: click, attr


$(button).attr('disabled', true);


<button onclick="this.disabled='disabled'" type...>


If you want to just target a button:

$('#buttonID').click(function(){
    $(this).attr('disabled', 'disabled');
});

If you want a function:

function diableButton(bID){
    $(bID).attr('disabled', 'disabled');
}

Call it using something like:

$('#myform').submit(function(){
    disableButton($('input[type=submit]', this));
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜