My requirement is on input change i want to check number of non empty input elements in a document. I have written some code but it does not seems to work. Please someone help me..
$('input').change(function()
{
var Auth=$(document).find('input').has(':data').length;
alert(Auth);
});
Here is my HTML Code
<table>
<tr><td><input type="text"></td></tr>
<tr><td><input type="text"></td></tr>
开发者_StackOverflow社区 <tr><td><input type="text"></td></tr>
<tr><td><input type="text"></td></tr>
<tr><td><input type="text"></td></tr>
<tr><td><input type="text"></td></tr>
<tr><td><input type="text"></td></tr>
<tr><td><input type="checkbox"></td></tr>
</table>
var count = 0;
$(document).find('input').each(function() {
if($(this).val() !== "") {
count++;
}
});
alert(count);
Inefficient but concise alternative
var count = $(document).find('input').filter(function() {
return $(this).val() !== "";
}).length;
alert(count);
Edit: Was too curious to benchmark the speeds, so here they are http://jsperf.com/counter-each-vs-filter-length (filter is ~ 8-10% slow for this size of input).
精彩评论