我有示例HTML代码:
<div class="category">
<div class="product">
<!-- some product info -->
<input type="text" value="0" /> <!-- it is quantity -->
</div>
<!-- ... other products -->
</div>
<!-- ... other categories -->
Run Code Online (Sandbox Code Playgroud)
所以我需要使用jQuery来获取所有类别,其中至少有一个产品具有正数量.
看起来像这样(在C#中)
var filteredCategories = categories.Where(c => c.Products.Any(p => p.Quantity !=0));
Run Code Online (Sandbox Code Playgroud)
如何使用jQuery做到这一点?
你可以使用.filter()jQuery方法
var filtered = $(".product").filter(function(){return this.find('input').val()>0});
Run Code Online (Sandbox Code Playgroud)
或者干脆
var filtered = $('.product:has(input[value!=0])');
Run Code Online (Sandbox Code Playgroud)
jQuery文档:具有Selector