【问题标题】:I want to send an value via ajax when the input is focused with keypress当输入以按键为焦点时,我想通过 ajax 发送一个值
【发布时间】:2013-07-26 02:17:19
【问题描述】:

我目前正在尝试编写聊天,但我的这段代码无法按我想要的方式运行。

$("#chat").focus(function() {
            $(document).keypress(function(e) {
                if (e.which == 13) {
                    $.post("send.php",
                            {
                                chat: $("#chat").val()
                            }

                            );

                }
            });
        });

“send.php”有效。页面重新加载,它不应该这样做并且我的值被删除。

希望得到您的帮助。 谢谢你的期待!

【问题讨论】:

    标签: php jquery mysql ajax chat


    【解决方案1】:

    使用return false;

    $("#chat").focus(function() {
       $(document).keypress(function(e) {
          if (e.which == 13) {
             $.post("send.php",{ chat: $("#chat").val() });
              return false;
           }
       });
    });
    

    【讨论】:

      【解决方案2】:

      在你的 if 语句之前输入 e.preventDefault(); 这将停止默认行为,在这种情况下是页面刷新。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 2016-07-02
        • 2020-11-03
        • 2019-09-27
        • 2012-08-10
        相关资源
        最近更新 更多