如何防止使用jQuery双击? [英] How to prevent a double-click using jQuery?

查看:184
本文介绍了如何防止使用jQuery双击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的按钮:

<input type="submit" id="submit" value="Save" />

在jQuery中,我正在使用以下内容,但仍允许双击:

Within jQuery I am using the following, but it still allows for double-clicks:

<script type="text/javascript">

$(document).ready(function () {

    $("#submit").one('click', function (event) {  

关于如何防止双击的任何想法?

Any idea on how I can prevent double-click?

推荐答案

jQuery的 one()将触发附加的事件处理程序每个绑定的元素一次,然后删除事件处理程序.

jQuery's one() will fire the attached event handler once for each element bound, and then remove the event handler.

如果由于某种原因不能满足要求,也可以在单击按钮后完全禁用该按钮.

If for some reason that doesn't fulfill the requirements, one could also disable the button entirely after it has been clicked.

$(document).ready(function () {
     $("#submit").one('click', function (event) {  
           event.preventDefault();
           //do something
           $(this).prop('disabled', true);
     });
});

应该注意,使用ID submit可能会导致问题,因为它会引用该元素覆盖form.submit函数.

It should be noted that using the ID submit can cause issues, as it overwrites the form.submit function with a reference to that element.

这篇关于如何防止使用jQuery双击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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