检查是否选中了JQuery移动复选框 [英] Check if a JQuery mobile checkbox is checked
问题描述
我有一个复选框和列表的列表。我想得到每个人的状态。
列表在这里:
I have a list of Checkboxes & I want to get the status of each one of them. The List is here :
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<input type="checkbox" name="Sunday" id="Sunday-1" class="custom" />
<label for="Sunday-1">Sunday</label>
<input type="checkbox" name="Monday" id="Monday-1" class="custom" />
<label for="Monday-1">Monday</label>
<input type="checkbox" name="Tuesday" id="Tuesday-1" class="custom" />
<label for="Tuesday-1">Tuesday</label>
</fieldset>
</div>
我通常使用以下引号检查复选框的状态:
I usually Check the checkbox's status using this quote:
var isChecked = $('#CheckboxID')。is(':checked');
推荐答案
但是在我的case现在,有没有一个方法来获取Jquery Mobile你可以做
You can do
var status = $('input[type="checkbox"]').filter('.custom').map(function(){
return $(this).is(':checked') ? 1 : 0;
});
然后状态数组
对于相同顺序的每个复选框,未检查的 0
和检查的 1
这不是很有用,因为你没有复选框的数组中的其他信息。
Then the status array
will have an entry for each checkbox in the same order, 0
for unchecked and 1
for checked. It's not much of useful as you have no other information of the checkbox in the array.
如果您要根据状态
执行操作,您可以使用 .each )
,如
If you want to perform operation based on the status
you can use .each()
instead, like
$('input[type="checkbox"]').filter('.custom').each(function(){
if($(this).is(':checked')){
// perform operation for checked
}
else{
// perform operation for unchecked
}
});
UPDATE
如果要使用复选框的名称和状态
构建对象数组
,您可以
If you want to build an array of objects
with the name and status
of the checkboxes you can do that with
var status = $('input[type="checkbox"]').filter('.custom').map(function(){
var name = $(this).attr('name');
if($(this).is(':checked'))
return { 'name':name, 'status' : 'Checked'};
else
return { 'name':name, 'status' : 'UnChecked'};
});
console.log(status);
演示: http://jsfiddle.net/joycse06/rL3Ze/
Read more on .each()
and .map()
这篇关于检查是否选中了JQuery移动复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!