我有一个web应用程序可以让用户创建帖子并写入数据库
然而,我有一个问题,当用户双击提交按钮,它创建2个帖子。将文件变成查询两次,是解决这个问题的任何方法。(我使用jquery Post到下一个文件并写入数据库)
$.post('add.php',{title:post.title.value,...}
在点击或提交处理程序中,您可以像这样禁用按钮:
$('#add_button').click(function() {
$(this).attr('disabled', true);
// Do your post stuff
$.post('add.php', {}, function(data) {
// Enable the button after the post, or on your success function
$(this).attr('disabled', false);
});
})
编辑:正如@roast所说,你也可以在你的JQuery对象上使用.prop
,而不是.attr
。
$('#buttonId').click(function() {
$(this).attr('disabled', 'disabled');
});
参考Happy Coding:)
使用javascript在第一次点击后禁用按钮。这样你就不会有任何重复的帖子。