开发者

Help needed in printing changed elements count in a document

开发者 https://www.devze.com 2023-03-18 22:11 出处:网络
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..

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消