使用JS/Jquery发布后禁用按钮 [英] Disable button after post using JS/Jquery

查看:85
本文介绍了使用JS/Jquery发布后禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个功能可以向表单按钮添加一个onclick事件,以禁用它们,以避免重复发布.

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</button>
</form>
<script> function disable(button){ $(button). ??? }</script>

所有想法还是小费?谢谢!

Ani idea or tip? thanks!

推荐答案

按钮必须是input元素(如果您不通过JavaScript提交表单). <button>元素没有属性type,对其进行设置将无效.
我还建议通过jQuery附加click处理程序:

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>

参考: click 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆