Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Enter键重新加载Chrome浏览器_Javascript_Jquery_Html_Google Chrome - Fatal编程技术网

Javascript Enter键重新加载Chrome浏览器

Javascript Enter键重新加载Chrome浏览器,javascript,jquery,html,google-chrome,Javascript,Jquery,Html,Google Chrome,我有一个inputfield和一个eventlistener按键,在Firefox服务器中一切都很好。如果我在Chrome中运行它并按enter键,浏览器将刷新,页面将再次加载 $('#iputField').keypress(function(e) { if (e.which == 13) { var message = $('#iputField').val(); } }); 试着说e.preventDefault()

我有一个inputfield和一个eventlistener按键,在Firefox服务器中一切都很好。如果我在Chrome中运行它并按enter键,浏览器将刷新,页面将再次加载

$('#iputField').keypress(function(e) {
    if (e.which == 13) {
        var message = $('#iputField').val();
    }
});

试着说
e.preventDefault()如果使用表单,可能需要调用
e.preventDefault()


尝试阻止默认行为:

 $('#iputField').keypress(function(e) {
     if(e.which == 13) {
        e.preventDefault().
        var message = $('#iputField').val();
     }
 });

可能表单正在提交,您必须停止默认事件传播以避免此情况

$('#iputField').keypress(function(e) {
  if(e.which == 13) {
    e.preventDefault();
    var message = $('#iputField').val();
  }
});