按下输入提交火灾2次

时间:2018-02-13 12:26:06

标签: javascript jquery html

按下输入提交火灾2次,但点击提交只触发一次,任何想法为什么在输入时提交火灾2次?。请帮忙

$(document).ready(function() {

  function loadchat() {
    $.post('message.php?action=get', function(response) {
      $('#getmsg').html(response);
    });
  }

  $("input").keyup(function(e) {
    if (e.which === 13) {
      e.preventDefault();
      $('form').submit();
    }
  });

  $('form').submit(function(t) {
    var msgs = $("input").val();
    t.preventDefault();
    $.post('message.php?action=sent&msg=' + msgs, function() {
      document.getElementById("myForm").reset();
    });
    return false;
  });
});

1 个答案:

答案 0 :(得分:1)

检查代码段与您的代码相同的工作正常 其他一些代码可能附带事件

但是如果你没有找到任何东西,那么使用它来避免双重提交

$("input").unbind('keyup').bind('keyup', function (e) {
  if (e.which === 13) {
    e.preventDefault();
    $('form').submit();
  }
});

希望这会对你有所帮助 感谢

 $(document).ready(function() {

    function loadchat() {
      $.post('message.php?action=get', function(response) {
        $('#getmsg').html(response);
      });
    }

    $("input").keyup(function(e) {
      if (e.which === 13) {
        e.preventDefault();
        $('form').submit();
      }
    });

    $('form').submit(function(t) {
      var msgs = $("input").val();
      t.preventDefault();
      alert(msgs);
      document.getElementById("myform").reset();
      return false;
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="myform" action="">
  <input type="text">
  <input type="submit" value="he"/>
</form>

相关问题