This might be the easiest question of the day.
i have a groups of checkboxes wit开发者_JS百科h similar id's (all starting with someid_
like someid_0
,someid_1
..)
i want to get all checked
checkboxes.
I have tried $('input:checkbox[id^="someid_"]:checked')
but it's not working?
this code is working check demo
http://jsfiddle.net/csTpG/
Markup
<input type="checkbox" id="someid_1" checked/>
<input type="checkbox" id="someid_2" checked/>
<input type="checkbox" id="someid_3" checked/>
<input type="checkbox" id="someid_4"/>
jQuery
var n = $('input:checkbox[id^="someid_"]:checked').length;
alert(n); // count of checked checkboxes
$('input:checkbox[id^="someid_"]:checked').each(function(){
alert($(this).attr("id"));});
<head>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
var isChecked = false;
function allSelected()
{
// this line is for toggle the check
isChecked = !isChecked;
//below line refers to 'jpCheckbox' class
$('input:checkbox.jpCheckbox').attr('checked',isChecked);
//OR,
//$('input:checkbox.jpCheckbox').attr('checked','checked');
}
</script>
</head>
<body>
<form>
Select All<input type="checkbox" id="selectAllCheckbox" onclick="allSelected()" /><br/>
A<input type="checkbox" id="jpCheckbox" class="jpCheckbox" /><br/>
B<input type="checkbox" id="jpCheckbox" class="jpCheckbox" /><br/>
C<input type="checkbox" id="jpCheckbox" class="jpCheckbox" /><br/>
</form>
</body>
I'm not sure if you can do a search on the ID attribute. it usually returns only one value. You can set the class to some_0 instead and then search or use a custom attribute like
<input type=checkbox customattr=some_1>
the code you tried is absolutely correct, may b the code ran before parsing the required elements so try,
$(document).ready(function(){$('input:checkbox[id^="someid_"]:checked')})
try this...
$('input:checkbox').filter('#someid').attr(":checked")
精彩评论