我有一些动态加载的复选框。它们具有相同的类名,但它们具有不同的title属性。我想找到所选的具有相同标题属性的复选框的数量。
<input class="checkme" type="checkbox" title="dynamically generated" />
答案 0 :(得分:1)
不要忘记伪选择器:checked
只过滤选中的复选框:
var count = $(":checkbox[title='something']:checked").length;
:checkbox
:选择所有元素的属性type
等于checkbox
[title='something']
:选择所有元素的属性title
等于something
:checked
:仅选择选中的复选框
上面的选择器之间没有空格意味着逻辑运算符AND
==&GT; ":checkbox[title='something']:checked"
答案 1 :(得分:0)
console.log($(':checkbox:checked[title=titlehere]').length)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' title='titlehere'>
<input type='checkbox' title='titlehere'>
<input type='checkbox' title=''>
<input type='checkbox' title='titlehere' checked>
<input type='checkbox' title='titlehere'>
<input type='checkbox' title=''>
<input type='checkbox' title='titlehere' checked>
&#13;
[title='yourtitlevalue']
获取带有该标题的复选框.length
获取数字:checked
仅选中复选框答案 2 :(得分:0)
HTML
<input type='checkbox' title='my-title' name='checkboxes' class='checkboxes'>
<input type='checkbox' title='my-title2' name='checkboxes' class='checkboxes'>
JS
$('.checkboxes').change(function () {
var checked = $(":checkbox[name='checkboxes']:checked");
checked.each(function(){
// check similar titles
}
}
答案 3 :(得分:0)
HTML
<input type='checkbox' title='my-title' name='checkboxes' class='checkboxes'>
<input type='checkbox' title='my-title2' name='checkboxes' class='checkboxes'>
<input type='checkbox' title='my-title2' name='checkboxes' class='checkboxes'>
<input type='checkbox' title='my-title2' name='checkboxes' class='checkboxes'>
的jQuery
function GetCheckboxesLength(title) {
var $checkboxes = $('.checkboxes');
var isChecked;
var checkboxTitle;
var length = 0;
$checkboxes.each(function () {
isChecked = $(this).is(':checked')
checkboxTitle = $(this).attr('title');
if (isChecked && checkboxTitle == title) {
length++;
}
});
return length;
}
GetCheckboxesLength('my-title2');