Javascript Javascript检查多个输入是否为空白,返回一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8068502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:36:20  来源:igfitidea点击:

Javascript check multiple inputs for blank, return one value

javascriptjqueryif-statement

提问by Greg Thompson

Ok guys, before I continue, I know what I want to do could be done with numerous variables etc. but I've gotta imagine that what I'm doing can be done much more simply. So what I have is a form that has 4 fields and I want to check if anyone of the four are blank. Then each one that is blank, I want to add a class and use the jquery UI effect 'shake' to notify. Then I want to get a true or false response, true being that all aren't blank and false being that any one of the 4 is blank. so what I have is.. HTML...

好的,在我继续之前,我知道我想要做的事情可以用许多变量等来完成,但我必须想象我正在做的事情可以更简单地完成。所以我有一个有 4 个字段的表单,我想检查这四个字段中是否有任何一个是空白的。然后每一个都是空白的,我想添加一个类并使用jquery UI效果'shake'来通知。然后我想得到一个真或假的回答,真的是所有的都不是空白的,假的是 4 个中的任何一个都是空白的。所以我所拥有的是.. HTML ...

<form name = "regin" id = "regin">
  <div id = "form_hold">
  <div><label>Username</label><input type="text" id = "Rusername" name = "Rusername" /><div class = 'verdiv' id = 'rvuname'></div></div>
  <div><label>Email</label><input type="text" id = "Remail" name = "Remail" /><div class = 'verdiv' id = 'rvemail'></div></div>
  <div><label>Password</label><input type="password" id = "Rpassword" name = "Rpassword" /><div class = 'verdiv' id = 'rvpass1'></div></div>
  <div><label>Confirm</label><input type="password" id = "Rpassword2" name = "Rpassword2" /><div class = 'verdiv' id = 'rvpass2'></div></div>
  </div>
  <button type="button" class = "thoughtbot" id = "registerButton">Register</button>
  </form>

and the javascript/jquery...

和 javascript/jquery ...

if($username == ''){
    $('#Rusername').parent().find('.verdiv').addClass('error');
    $('#Rusername').parent().effect("shake", { times:3 }, 50);
}
if($email == ''){
    $('#Remail').parent().find('.verdiv').addClass('error');
    $('#Remail').parent().effect("shake", { times:3 }, 50);
}   
if($password == ''){
    $('#Rpassword').parent().find('.verdiv').addClass('error');
    $('#Rpassword').parent().effect("shake", { times:3 }, 50);
}
if($checkval == ''){
    $('#Rpassword2').parent().find('.verdiv').addClass('error');
    $('#Rpassword2').parent().effect("shake", { times:3 }, 50); 
}

Now is there one top level function to 'test' each one of these statements to see if they're true? Thanks.

现在是否有一个顶级函数来“测试”这些陈述中的每一个,看看它们是否正确?谢谢。

回答by Blender

You could loop:

你可以循环:

$('#Rusername, #Remail, #Rpassword, #Rpassword2').each(function() {
  if ($(this).val() == '') {
    $(this).parent().effect('shake', {times: 3}, 50).find('.verdiv').addClass('error');
  }
});

But ideally, I would add a class to their parent element and simplify the selector to just this:

但理想情况下,我会向它们的父元素添加一个类,并将选择器简化为:

$('.parent_class input');

EDIT: The code was not complete - I removed a left curly brace.

编辑:代码不完整 - 我删除了一个左花括号。

回答by Lazarus

What about:

关于什么:

$("form input:text").each(function() {
    if ($(this).val() == "") {
       $(this).parents().find('.verdiv').addClass('error');
       $(this).parent().effect("shake", { times:3 }, 50);
    }
});

--EDIT--

- 编辑 -

To answer your additional question, you could add an attribute to the fields, e.g. data-type="email", etc. then modify the javascript thus:

要回答您的其他问题,您可以向字段添加一个属性,例如 data-type="email" 等,然后这样修改 javascript:

$("form input:text").each(function() {
    var isValid = new Function("value", "validate" + $(this).attr("data-type") + "(value);");
    if ($(this).val() == "" && !isValid($(this).val())) {
       $(this).parents().find('.verdiv').addClass('error');
       $(this).parent().effect("shake", { times:3 }, 50);
    }
});

You'd need to add functions for "validateemail", "validatetext", etc. that return a true for a valid field and false for an invalid field.

您需要为“validateemail”、“validatetext”等添加函数,这些函数对有效字段返回 true,对无效字段返回 false。

回答by Amritpal Singh

    $('input [type=text]').each(function(event){
    if($(this).val()=='')
    {
    //Do here shaking and error showing.
    }
    });

回答by Sam

You could add a data-validate=trueattribute to each input, then do it like so:

您可以data-validate=true为每个输入添加一个属性,然后这样做:

function validate() {
var valid = true;
$("[data-validate]").each(function () {
    var el = $(this);
    if(el.val() == "") {
        el.parent().find('.verdiv').addClass('error');
        el.parent().effect("shake", { times:3 }, 50);
        valid = false;
    }
});

return valid
}

回答by Rory McCrossan

Put the same class on each of the input fields to be validated, for example validatefield.

在要验证的每个输入字段上放置相同的类,例如validatefield

Then assuming that $usernameand $emailare the values of each input field, this should work:

然后假设$username$email是每个输入字段的值,这应该有效:

var valid = true;
$(".validatefield").each(function() {
    if ($(this).val() == "") {
        $(this).parent().find('.verdiv').addClass('error');
        $(this).parent().effect("shake", { times:3 }, 50);
        valid = false;
    }
});

回答by lqez

Little bit complex and dirty, but may works.

有点复杂和肮脏,但可能有效。

var items = { 'username':'Rusername', 'email':'Remail', 'password':'Rpassword', 'checkval':'Rpassword2' };
$.each( items, function( val, id ) {
    eval('if($'+val+' == \'\') { ....copy your codes here... }');
});