如何使用jquery获得下拉列表中所有元素的计数
How to get count of all elements which are in dropdown list using jquery
我有一个下拉列表,像
<select id="ddlProjects">
<option value="315">resproject</option>
<option value="320" style-"display:inline">newheavn</option>
<option value="395" style="display:inline">cealon sales</option>
<option value="395" style="display:inline">cealon sales</option>
</select>
现在,我想计数的所有元素是"display:inline"的变化,我怎么能得到它。
在jquery中使用属性选择器获取下拉列表中选项的长度
$('#ddlProjects').change(function(){
var len = $(this).find('option[style="display:inline"]').length
console.log(len)
});
默认情况下,<option>
的display
属性设置为inline
如果您仍然需要根据此属性进行过滤,请使用
$('#ddlProjects').change(function(){
var count = $('option',this).filter(function(){
var css =$(this).attr('style');
return css !=undefined && css.indexOf('display:inline')>-1;
}).length;
console.log(count);
});
尝试在此上下文中使用
.filter()
,
$('#ddlProjects').change(function(){
var count = $(this).children().filter(function(){
return $(this).css('display') === "inline"
}).length;
console.log(count);
})
在这种情况下尝试使用attribute contains selector
,因为option元素的默认显示属性是inline。
$('#ddlProjects').change(function(){
var count = $('option[style*="display:inline"]',this).length;
console.log(count);
});
演示$('#ddlProjects option').filter(function() {
return $(this).css('display') === 'inline';
}).length;
相关文章:
- 可以't附加html元素jquery
- 循环遍历元素jquery选择器
- 在ajax加载的元素jquery上包含现有的jquery函数
- 如何删除元素列表中的类并添加到另一个元素 Jquery.
- 获取单击的类元素 jQuery 的名称值
- 获取上一个元素 jQuery 的内容
- 根据另一个元素的更改获取一个元素(jquery)
- 区分不同的可拖放元素(jQuery 拖放)
- 除了最后一个<td>元素-Jquery
- 获取子元素jQuery的单选按钮值
- 捕获元素jquery外部的mousedown事件
- .append到非DOM元素jQuery
- 获取其父元素的精确子元素.Jquery
- 获取重复结构中的特定元素jquery
- 捕获隐藏输入元素jQuery的值
- 使用onclick函数获取点击的元素jquery
- 返回父元素 jquery 中的输入值
- 按属性查找元素 jQuery
- 选择元素 jQuery
- 按子元素 - jQuery 的数据 ID 对 li 进行排序