jquery - 在 jQuery 中,如何防止多次点击多次执行我的代码?

标签 jquery onclick click mouseevent

例如,如果我有以下代码:

$("#divTest").append(variable);

预期的目标是单击按钮后显示一条信息,如何处理不耐烦或感觉需要在执行代码之前多次单击(从而生成多个副本)的用户?

如果需要的话我会澄清。

谢谢!

最佳答案

看看 jQuery .one功能。这只会允许点击处理程序使用一次。

$("#button").one("click",function() { /* -- Your code here -- */ });
<小时/>

或者,如果按钮实际上是 inputbutton 标记,您可以设置 disabled 属性。以下是来自 Mozilla Developer Network (MDN) 的更多信息关于disabled 属性。

This Boolean attribute indicates that the form control is not available for interaction. In particular, the click event will not be dispatched on disabled controls. Also, a disabled control's value isn't submitted with the form.

关于jquery - 在 jQuery 中,如何防止多次点击多次执行我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16301455/

相关文章:

javascript - 在javascript中将字符串转换为数组

javascript - 插入到 DOM 的元素中未触发 Onclick

javascript - 如何使用DataTables处理onclick事件?

javascript - 如何让 CSS 下拉菜单保持打开状态,即使在菜单外单击也是如此?

jquery - 标签触发单击功能两次

jquery - 选择具有一定内容的元素作为直接子节点

javascript - 如何正确编写递归 jquery promise

javascript - 字符串数组的字符串操作

events - 处理树面板 Extjs 4 上的 itemclick 事件

jquery - td.click 在 jQuery 中不会触发