javascript 如果在 jQuery 中为空,则检查数组中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18744090/
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-10-27 13:04:02  来源:igfitidea点击:

Checking elements in array if empty in jQuery

javascriptjquery

提问by AloneInTheDark

i have an array of input fields which will contain some information. I should check them if one of them are empty.

我有一个包含一些信息的输入字段数组。如果其中一个是空的,我应该检查它们。

jQuery Code:

jQuery 代码:

   $("#NewUserBtn").click(function () {

            var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

            for (i = 0; i < zorunluAlan.length; i++) {
                if (//What should i write here?) {
                    alert("Please fill all input fields!");
                    break;
                }
            }
    });

回答by Dipak Ingole

Use,

利用,

$("#NewUserBtn").click(function () {

            var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

            for (i = 0; i < zorunluAlan.length; i++) {
                if ($(zorunluAlan[i]).val().trim().length == 0) {
                    alert("Please fill all input fields!");
                    return false;
                }
            }
    });

回答by David

Assuming you're using jQuery, you need to put !$(zorunluAlan[i]).val()as your condition.

假设您使用的是 jQuery,则需要将其!$(zorunluAlan[i]).val()作为条件。

回答by letiagoalves

You can do it in a cleaner way using jQuery filterfunction:

您可以使用 jQueryfilter函数以更简洁的方式完成此操作:

var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

var $empty = $(zorunluAlan.join(",")).filter(function() {
    return ($(this).val().length === 0);
});

if($empty.length > 0) {
   alert("Please fill all input fields!");
}

回答by Dave Haigh

An alternative method using each

使用每个的替代方法

var zorunluAlan = $("#YeniAd, #YeniSoyad, #YeniEposta, #YeniEpostaOnay, #YeniSifre, #YeniSifreOnay"),
        invalid = false;
    if(zorunluAlan.length) {
      zorunluAlan.each(function() {
        var thisValue = $(this).val();
        if(thisValue == "" || !thisValue){
          invalid = true;
        }    
      });
    }
    if(invalid){
      alert("Please fill all input fields!");
    }

回答by epascarello

Basic idea using jQuery to add an error class

使用jQuery添加错误类的基本思路

$("#a,#b,#c")
    .removeClass("error")
    .filter( 
        function(){
            return !this.value.length;
        }
    )
   .addClass("error");

http://jsfiddle.net/m3WA8/

http://jsfiddle.net/m3WA8/

It could be simplified by using a class/attribute instead of the string of ids.

可以通过使用类/属性而不是 id 字符串来简化它。

回答by Niet the Dark Absol

It's really easy, even in Vanilla JS

这真的很容易,即使在Vanilla JS 中

document.getElementById('NewUserBtn').onclick = function() {
    var zorunluAlan = ["YeniAd","YeniSoyad","........"],
        l = zorunluAlan.length, i;
    for( i=0; i<l; i++) {
        if( !document.getElementById(zorunluAlan[i]).value) {
            alert("Please fill all input fields! ["+zorunluAlan[i]+" is empty]");
            return false;
        }
    }
    return true;
}

Then again, it's even easier in HTML:

再说一次,在 HTML 中它更容易:

<input type="text" name="YeniAd" REQUIRED />

The attribute doesn't have to be in uppercase, I just made it so to make it more obivous ;)

该属性不必是大写的,我只是为了使它更明显;)

回答by A. Wolff

   $("#NewUserBtn").click(function () {

       var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

       for (i = 0; i < zorunluAlan.length; i++) {
           if (!$.trim($(zorunluAlan[i]).val()).length) {
           alert("Please fill all input fields!");
           break;
           }
       }
   });