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;
有帮助吗?

解决方案

Try:

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

其他提示

use:

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

Working Demo

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top