Jquery 如何在不刷新页面的情况下重置.one()
我有一个submit按钮,我附加了一个Jquery 如何在不刷新页面的情况下重置.one(),jquery,ajax,refresh,partial-page-refresh,Jquery,Ajax,Refresh,Partial Page Refresh,我有一个submit按钮,我附加了一个.one()鼠标事件,否则如果你不小心双击它,它会发送两次信息 唯一的问题是,信息发送后,我会刷新包含的div(我不知道要刷新整个页面),然后显示新信息。但是,如果您要添加更多信息,它将不允许您再次单击“提交”按钮,因为它已被单击 当我刷新div时,我想它会重置……但它没有。我该如何解决这个问题 塔克斯 编辑 在乔的帮助下,我做到了这一点。但它仍然在双击时发射两次。我做对了吗 var clickActive = false; $("body").on
.one()
鼠标事件,否则如果你不小心双击它,它会发送两次信息
唯一的问题是,信息发送后,我会刷新包含的div
(我不知道要刷新整个页面),然后显示新信息。但是,如果您要添加更多信息,它将不允许您再次单击“提交”按钮,因为它已被单击
当我刷新div
时,我想它会重置……但它没有。我该如何解决这个问题
塔克斯
编辑
在乔的帮助下,我做到了这一点。但它仍然在双击时发射两次。我做对了吗
var clickActive = false;
$("body").on("click", '.post_comment_button', function(e){
e.preventDefault();
if(clickActive) return;
else {
clickActive = true;
var post = $(this).parents('.update_comment').parents('#post_comment');
var anchor = $(post).siblings('.comment_list').find('ul:first');
var comment = $(this).parents('.update_comment').children('textarea.post_comment').val();
var user = $(this).parents('.update_comment').children('input.user').val();
var msg_id = $(this).parents('.update_comment').children('input.message_id').val();
if (comment == '') loadComments();
else {
$.post('messages.php', {
comment: comment,
message_id: msg_id,
post_comment: 'true' }, function(data) {
//create new comment//
$('body').append(data);
var newcomment = "<li><div class='comment_container'><div class='date'>less than 1 minute ago</div><div class='name'>" + user + " </div><div class='info_bar'><div class='edit_comment'><a href='#' class='comment_edit'>Edit</a></div><span>|</span><a href='#' class='delete_comment'>Delete</a></div><div class='fadeOut_comment'><div class='posted_comment'> " + nl2br(htmlEntities(comment.trim())) + " </div></div></li>";
$(post).slideUp(400);
$(newcomment).fadeIn(500, function() {
loadComments();
}).appendTo(anchor);
clickActive = false;
});
}
}
});
var clickActive=false;
$(“正文”)。在(“单击”按钮上。发布注释按钮),函数(e){
e、 预防默认值();
如果(单击活动)返回;
否则{
单击Active=true;
var post=$(this).parents('.update_comment')。parents('#post_comment');
var-anchor=$(post).同级('.comment_list').find('ul:first');
var comment=$(this).parents('.update_comment').children('textarea.post_comment').val();
var user=$(this).parents('.update_comment').children('input.user').val();
var msg_id=$(this).parents('.update_comment').children('input.message_id').val();
如果(comment='')loadComments();
否则{
$.post('messages.php',{
评论:评论,,
消息\u id:msg\u id,
post_注释:'true'},函数(数据){
//创建新注释//
$('body')。追加(数据);
var newcomment=“不到1分钟前”+user+“|”+nl2br(htmlEntities(comment.trim())+“ ”;
美元(邮政).slideUp(400);
$(newcomment).fadeIn(500,function(){
loadComments();
}).附件(锚);
clickActive=false;
});
}
}
});
另一种解决方案是禁用该按钮,这样用户就不能连续单击两次。
使以下逻辑适应您的代码
//Bind a click event handler to your button
$("body").on("click", "you_button_selector", function(e)
{
//Prevent any default behaviour, we're dealing with this ourselves
e.preventDefault();
//First, disable the button
$("your_button_selector").attr("disabled", "disabled");
//Then, make your ajax call
$.ajax(
{
url: "http://example.com",
success: function(data)
{
//When your ajax call returns, enable the button again
$("your_button_selector").removeAttr("disabled");
}
});
});
这里回答:如果将一个事件放入函数中,它将再次工作。我认为这个问题是重复的:你不应该使用
one()
,而应该使用on()
,然后使用off()
禁用按钮,直到内容更新。我读了这个问题,thanx。我把它放在一个函数中…但它仍然不能工作,在它被使用一次之后我如何调用该函数?与off()
tho的问题相同。我怎么才能让它让我再飞一次呢?天才!完全奏效了。我甚至不需要在重新加载div
时再次启用该按钮。thanx dude…还是dudette