简体   繁体   English

当输入字段为空时,使用jQuery来阻止表单提交

[英]Using jQuery to prevent form submission when input fields are empty

The solution should be pretty straightforward. 解决方案应该非常简单。 I'm trying to prevent the form from submitting properly when no value is found within the input boxes. 我想在输入框中找不到任何值时阻止表单正确提交。 Here's my JSFiddle: http://jsfiddle.net/nArYa/7/ 这是我的JSFiddle: http//jsfiddle.net/nArYa/7/

//Markup //标记

<form action="" method="post" name="form" id="form">
<input type="text" placeholder="Your email*" name="email" id="email">
<input type="text" placeholder="Your name*" autocomplete=off name="name" id="user_name"
<button type="submit" id="signup" value="Sign me up!">Sign Up</button>
</form>

//jQuery // jQuery的

if ($.trim($("#email, #user_name").val()) === "") {
    $('#form').submit(function(e) {
        e.preventDefault();
        alert('you did not fill out one of the fields');
    })
} 

As you can see in the JSFiddle, the problem is that when I type something into both fields, the alert box STILL pops up. 正如您在JSFiddle中看到的那样,问题是当我在两个字段中输入内容时,会弹出警告框STILL。 I'm having a hard time figuring out why. 我很难搞清楚原因。 Is there something wrong within my if($.trim($"#email, #user_name").val()) === "") ? 在我的if($ .trim($“#email,#user_name”)。val())===“”)中是否有问题?

Two things, #1 the check for empty fields should happen on every attempt of submit, #2 you need to check each field individually 两件事,#1检查空字段应该在每次提交尝试时发生,#2你需要单独检查每个字段

$('#form').submit(function() {
    if ($.trim($("#email").val()) === "" || $.trim($("#user_name").val()) === "") {
        alert('you did not fill out one of the fields');
        return false;
    }
});

Updated fiddle 更新了小提琴

Your check occurs on page load. 您的检查在页面加载时发生。 You need to check the field when the form is submitted. 提交表单时需要检查字段。

$('#form').submit(function(e) {
    if ($.trim($("#email, #user_name").val()) === "") {
        e.preventDefault();
        alert('you did not fill out one of the fields');
    }
});

HTML5: use required in input tag HTML5:输入标记中需要使用

  • A boolean attribute. 布尔属性。

  • Input field must be filled out before submitting the form. 提交表单之前必须填写输入字段。

  • Works with text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file. 适用于文本,搜索,网址,电话,电子邮件,密码,日期选择器,号码,复选框,广播和文件。

     <input required type='text'...> 

w3schools hint w3schools提示

I guess that this will help: 我想这会有所帮助:

$('#form').submit(function(e) {
    e.preventDefault();
    $("#email, #user_name").each(function(){
        if($.trim(this.value) == ""){
            alert('you did not fill out one of the fields');
        } else {
            // Submit 
        }
    })
})

Put your if statement inside the callback: 将if语句放在回调中:

$('#form').submit(function(e) {
    if ($.trim($("#email").val()) === "" || $.trim($("#user_name").val())) {
        e.preventDefault();
        alert('you did not fill out one of the fields');
        //You can return false here as well
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM