Jquery选择器用于查找非空输入的计数 [英] Jquery selector to find out count of non empty inputs

查看:84
本文介绍了Jquery选择器用于查找非空输入的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML DOM

I have the below HTML DOM

      <div id="container>
        <p data-bind="visible:ShowPostCode()">
    <label class="field-label" for="postcode">Postcode</label>
    <input id="txtPostCode" data-bind="value:PostCode, valueUpdate: &quot;afterkeydown&quot;" class="field-stretch" type="text">
    </p>

     <p data-bind="visible:ShowDateOfBirth()">
    <label class="field-label" for="dateofbirth">Date of birth</label>
    <input data-bind="value:DateOfBirth, valueUpdate: &quot;afterkeydown&quot;" class="field-stretch" type="text">
    </p>

             <p style="display: none;" data-bind="visible:ShowtelephoneNumber()">
    <label class="field-label" for="telephoneNumber">Telephone number</label>
    <input data-bind="value:DrivingLicenceNumber, valueUpdate: &quot;afterkeydown&quot;" class="field-stretch" type="text">
    </p>
   </div>

我想获得非空输入框的数量。

I would like to get the no of non-empty input boxes.

这就是我试过的事情

$('#container input[type="text"][value!=""]').length

$('#container input[type="text"]').filter('input[value!=""]').length

即使我输入一些值,它总是显示为零。我试过的这些选择器有什么问题,为什么它不起作用?

even when i input some values it is always showing as zero . what is wrong with these selectors i tried and why it is not working?

推荐答案

您可以传入过滤器函数而不是选择器:

You can pass in a filter function rather than a selector:

$('#container input[type="text"]').filter(function () {
    return !!this.value;
}).length;

这篇关于Jquery选择器用于查找非空输入的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆