使用jquery获取属性的数量

时间:2014-09-25 19:05:10

标签: jquery

我有一个包含许多元素的页面:

<td>
  <input id="Data_0__IsDeleted" type="hidden" value="False" name="Data[0].IsDeleted" data-val-required="The IsDeleted field is required." data-val="true">
 <input id="Data_0__MobileId" type="hidden" value="1" name="Data[0].MobileId" data-val-required="Required" data-val="true">
 <input id="Data_0__CLI" type="hidden" value="07777777777" name="Data[0].CLI">
  <input id="Data_0__TariffName" type="hidden" value="Test Service Template" name="Data[0].TariffName">
  <input id="Data_0__TariffId" type="hidden" value="2" name="Data[0].TariffId" data-val-required="The TariffId field is required." data-val-number="The field TariffId must be a number." data-val="true">
  <input id="Data_0__NewTariffName" type="text" value="" name="Data[0].NewTariffName" data-val-required="Required" data-val="true">
  <span class="field-validation-valid help-inline valid_error" data-valmsg-replace="true" data-valmsg-for="Data[0].TariffId"></span>
</td>
...
<td>
  <input id="Data_1__IsDeleted" type="hidden" value="True" name="Data[1].IsDeleted" data-val-required="The IsDeleted field is required." data-val="true">
  <input id="Data_1__MobileId" type="hidden" value="2" name="Data[1].MobileId" data-val-required="Required" data-val="true">
  <input id="Data_1__CLI" type="hidden" value="07888888888" name="Data[1].CLI">
  <input id="Data_1__TariffName" type="hidden" value="Test Service Template" name="Data[1].TariffName">
  <input id="Data_1__TariffId" type="hidden" value="2" name="Data[1].TariffId" data-val-required="The TariffId field is required." data-val-number="The field TariffId must be a number." data-val="true">
  <input id="Data_1__NewTariffName" type="text" value="" name="Data[1].NewTariffName" data-val-required="Required" data-val="true">
  <span class="field-validation-valid help-inline valid_error" data-valmsg-replace="true" data-valmsg-for="Data[1].TariffId"></span>
</td>

我希望使用&#34; _IsDeleted&#34;返回输入元素的数量。值设置为false。我本以为会有这样的事情。

$("[id^='Data_'][id$='__IsDeleted']").val("False").length)

1 个答案:

答案 0 :(得分:1)

val将查询或设置值,但不会按此过滤。您可以尝试在选择器中使用其他属性选择器。

$("[id^='Data_'][id$='__IsDeleted'][value=False]").length

或者如果你想要更复杂的匹配:

$("[id^='Data_'][id$='__IsDeleted']").filter(function(index) {
    //Your code here, return true if you want it to pass.
    return $(this).val() === "False";
}).length