简体   繁体   English

jQuery 计算有值的输入字段的数量

[英]jQuery count number of input fields that have a value

When a value is entered in an input field, I want to count how many input fields in that row have a value in total.在输入字段中输入值时,我想计算该行中总共有多少输入字段具有值。

<div class="row">
    <input type="text" class="input input1" />
    <input type="text" class="input input2" />
    <input type="text" class="input input2" />
</div>

<div class="row">
    <input type="text" class="input input1" />
    <input type="text" class="input input2" />
    <input type="text" class="input input2" />
</div>

I'm trying following but I think problem is with the loop.我正在尝试关注,但我认为问题出在循环上。

$('.input').keyup(function () {

    var field = $(this).parent().children('.input');
    var count = 0;

    $(field).each(function (i) {
        var len = $(field).val().length;        
        if (len > 0 ) {
            count++;
        }
    });

  alert(count);

});

JSFiddle link: http://jsfiddle.net/18cpotw6/ JSFiddle链接: http://jsfiddle.net/18cpotw6/

With $(field).val() , you're retrieving the value of the first element in the field jQuery collection.使用$(field).val() ,您将检索field jQuery 集合中第一个元素的值。 Use $(this).val() instead:使用$(this).val()代替:

 $('.input').keyup(function() { var field = $(this).parent().children('.input'); var count = 0; $(field).each(function() { if ($(this).val()) { count++; } }); console.log(count); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <input type="text" class="input input1" /> <input type="text" class="input input2" /> <input type="text" class="input input2" /> </div> <div class="row"> <input type="text" class="input input1" /> <input type="text" class="input input2" /> <input type="text" class="input input2" /> </div>

You can also remove the i parameter, since it's not being used.您也可以删除i参数,因为它没有被使用。

You looped through the fields you have found but used the same field variable which contains all the fields to find the values.您遍历了找到的字段,但使用了包含所有字段的相同字段变量来查找值。 Instead you have to do this -相反,你必须这样-

$('.input').keyup(function () {

    var field = $(this).parent().find('.input');
    var count = 0;

    $(field).each(function (i) {
        var len = $(this).val().length;     
        if (len > 0 ) {
            count++;
        }
    });

  alert(count);

});

Fiddle: http://jsfiddle.net/ashhaq12345/yxrwdkfL/3/小提琴: http://jsfiddle.net/ashhaq12345/yxrwdkfL/3/

From reference to this post , you can also create a custom selector and then only get input which has values.通过参考这篇文章,您还可以创建一个自定义选择器,然后只获取具有值的input

 jQuery.expr[':'].hasValue = function(el, index, match) { return el.value;= ""; }. $('.input').keyup(function() { console.log($(this).parent():find('input.hasValue');length); });
 <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <div class="row"> <input type="text" class="input input1" /> <input type="text" class="input input2" /> <input type="text" class="input input2" /> </div> <div class="row"> <input type="text" class="input input1" /> <input type="text" class="input input2" /> <input type="text" class="input input2" /> </div>

You can use filter .您可以使用过滤器 You first select the elements, and then filter them on if they have a value or not:您首先 select 元素,然后过滤它们是否有值:

 function example(){ const $row = $('.row'); const length = $row.find('input').filter(function() { return this.value.length;== 0. });length. console;log(length); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <input class="someSelection" /> <input class="someSelection" value="foo" /> <input class="someSelection" /> <input class="someSelection" value="bar"/> </div> <button onclick="example()">Press me!</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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