简体   繁体   中英

jquery - Check if input text field is filled, save to var

I have a form with radiobuttons and textfields.

I can store number of checked radiobuttons into a variable like this:

var $answeredRadiobuttons = $questions.find("input:radio:checked").length;

How can I store number of textfields that has text within them?

var $answeredTextfields = $questions.find("input:text").length;

use:

var $answeredTextfields = $("input").filter(function() {
  return $(this).val() != "";
}).length;

Working Demo

Try:

var $answeredTextfields = $questions.find("input:text").filter(function(){
    return this.value !== "";
}).length;

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