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

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

问题描述

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

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?

我在调查中使用 HTML、PHP 5.2.9 和 jQuery.

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

推荐答案

你可以使用诸如

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

在阅读原始帖子的评论时,为了使其更有用,并允许人们在完成所有字段后按 Enter:

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天全站免登陆