通过点击Enter阻止用户提交表单 [英] Prevent users from submitting a form by hitting Enter

查看:162
本文介绍了通过点击Enter阻止用户提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上进行了一项调查,似乎有一些问题与用户点击进入(我不知道为什么),并且在不点击提交按钮的情况下意外提交调查(表单)。有没有办法来防止这种情况?



我在调查中使用HTML,PHP 5.2.9和jQuery。

解决方案

您可以使用一种方法,例如

  $(document).ready(function(){
$(window).keydown(function(event){
if(event.keyCode == 13){
event.preventDefault();
return false;
}
});
});

在阅读原始文章的评论时,为了使其更易于使用,并允许用户按输入,如果他们已经完成了所有的字段:

 函数validationFunction(){
$('输入')。each(function(){
...

}
if(good){
return true;
}
返回false;
}

$(document).ready(function(){
$(window).keydown(function(event){
if( (event.keyCode == 13)&&(validationFunction()== false)){
event.preventDefault();
return false;
}
}) ;
});


I have a survey on a website, and there seems to be some issues with the users hitting enter (I don't know why) and accidentally submitting the survey (form) without clicking the submit button. Is there a way to prevent this?

I'm using HTML, PHP 5.2.9, and jQuery on the survey.

解决方案

You can use a method such as

$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});

In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:

function validationFunction() {
  $('input').each(function() {
    ...

  }
  if(good) {
    return true;
  }
  return false;
}

$(document).ready(function() {
  $(window).keydown(function(event){
    if( (event.keyCode == 13) && (validationFunction() == false) ) {
      event.preventDefault();
      return false;
    }
  });
});

这篇关于通过点击Enter阻止用户提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆