繁体   English   中英

jQuery单击发生得太快,无法刷新数据库的结果吗?

[英]jquery click happens too fast to refresh results from database?

我正在制作一个简单的喊话框,当有人发布消息时,该消息会被添加到数据库中。 我做了一个刷新按钮,它填充了文本区域中的所有消息。 我尝试在此人输入消息后在刷新按钮上调用.click(),但我认为它发生得太快并且无法在数据库中捕获新消息。 如果添加警报以给它更多时间,我可以工作。 有没有更好的办法?

$("#shoutBtn").click(function(event){
   event.preventDefault();
   $.post("includes/shoutBoxPost.php", {"message": $("#messageBox").val(),
                                     "name": $("#userNameWelcome").html()});
   $("#messageBox").val("");
   $("#shoutRefresh").click();  
});

我将代码更改为长手ajax方式,并且它的工作原理非常感谢大家

    $.ajax({
      type: 'POST',
      data: {"message": $("#messageBox").val(),
            "name": $("#userNameWelcome").html()},
      url : 'includes/shoutBoxPost.php',
      success : function(data) {
          $("#messageBox").val("");
          $("#shoutRefresh").click();
      }
    });

请参阅jQuery .post的文档。 您可以定义仅在发布请求成功返回后才触发的成功回调。

jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

$.post("includes/shoutBoxPost.php", 
    {"message": $("#messageBox").val(), "name": $("#userNameWelcome").html()},
    function( data, textStatus, jqXHR ) {
        $("#shoutRefresh").click();
    }
);

您可以在帖子中添加回叫功能

$.post( "includes/shoutBoxPost.php",{"message": $("#messageBox").val(), "name": $("#userNameWelcome").html()})
.done(function( data ) {
$("#messageBox").val("");
$("#shoutRefresh").click();
  });

您需要等待您的帖子完成,然后发送成功消息(通过JSON),然后执行刷新操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM