简体   繁体   中英

Knowing if all input boxes are empty in ES5 (what is the ES5 equivalent of this ES6?)

Trying to know if all input boxes with an ID starting with 'txtYear_' are empty. I have the following ES6 code, but I would like a ES5 equivalent:

Thanks!

 let allNotEmpty = Array.from($("[id^='txtYear_']")).every(function(e) { return e.value !== ""; }) console.log(allNotEmpty); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="txtYear_1" value="1"> <input id="txtYear_2" value="2"> <input id="txtYear_3" value="3"> <input id="txtYear_4" value="4"> <input id="txtYear_5" value="5"> <input id="txtYear_6" value="6"> <input id="txtYear_7" value="7"> 

You can use call to apply Array#every to any array-like object:

var allNotEmpty = Array.prototype.every.call($("[id^='txtYear_']"), function(e) {
  return e.value !== "";    
});

Side note: selectors like $("[id^='txtYear_']") are a good sign you should be using a class instead.

You could use the .each function from jQuery.

 var allNotEmpty = true; $("[id^='txtYear_']").each(function(i, el) { allNotEmpty = allNotEmpty && el.value !== ""; }); console.log(allNotEmpty); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="txtYear_1" value="1"> <input id="txtYear_2" value="2"> <input id="txtYear_3" value="3"> <input id="txtYear_4" value="4"> <input id="txtYear_5" value="5"> <input id="txtYear_6" value="6"> <input id="txtYear_7" value="7"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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