jQuery 在 .ajax() 之前只允许单击一次
我试图允许只单击一次按钮,然后通过 ajax 提交一些数据。我面临的问题是用户可以点击50x并且每次都POST提交数据?
jQuery("#id").unbind('click');
jQuery.ajax({
type: "POST",
url: ajax_url,
data: ajax_data,
cache: false,
success: function (html) {
location.reload(true);
}
});
如何确保用户点击 #ID
100 次时数据仅提交一次?然后重新启用#ID?
I am trying to allow a button to be clicked only once and then some data be submitted via ajax. The problem I am facing is that a user can click 50x and the data is POST submitted each time ?
jQuery("#id").unbind('click');
jQuery.ajax({
type: "POST",
url: ajax_url,
data: ajax_data,
cache: false,
success: function (html) {
location.reload(true);
}
});
How can I ensure that if a user clicks #ID
100x - that the data is only submitted once ? And then #ID is re-enabled ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在 jQuery 中使用
.one()
函数。请记住,这将完全删除点击事件,即使您的ajax出现错误,您仍然无法再次点击它。
You could use the
.one()
function in jQuery.Bear in mind this will completely remove the click event, even if you have an error with your ajax, you still won't able to click it again.
只需禁用该按钮
,然后在成功功能中启用它
just disable the button
and then in the success function enable it
最简单的方法是使用一个标志,当成功触发时该标志会重置:
Easiest way would be to use a flag which gets reset when the success is fired:
您可以禁用控制,或使用众多模态库之一来显示旋转轮,
请参阅此 Jquery 模式对话框问题
You can disable to control, or use one of the many modal libraries to show the spinning wheel
see this Jquery modal dialog question on SO
您可以在单击事件上禁用该按钮,并在 ajax 请求完成时重新启用它。
You could disable the button on click event and enable it back when ajax request is completed.
在您的点击事件中,您可以禁用该按钮,然后在 ajax 事件的成功函数中重新启用该按钮。
另一种选择是在被单击的元素上设置一个参数,以指示单击了按钮,然后检查是否设置了该参数,如果设置了,则不发送 ajax 请求,如果没有,则发送它。 ajax 完成后,您可以再次取消设置该参数以允许其运行。
In your click event you could disable the button and then re-enable the button in the success function of the ajax event.
Another option would be to set a parameter on the element that is being clicked to indicate the button was clicked and then check to see if it is set if it is don't send the ajax request if not then do send it. Once the ajax is done you can unset the parameter again to allow it to be run.
试试这个:
其中:#submit_button是你想要禁用的元素的ID,
该代码将禁用单击提交按钮
try this:
where: #submit_button is id of the element U want to disable
that code will disable clicking on the submit button