Javascript 检查是否选中了 JQuery 移动复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11138898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a JQuery mobile checkbox is checked
提问by Sana Joseph
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');
var isChecked = $('#CheckboxID').is(':checked');
But in my case now, is there a method to get the status of "Jquery Mobile" checkboxes all at once ?
但就我而言,是否有一种方法可以一次性获取“Jquery Mobile”复选框的状态?
回答by Prasenjit Kumar Nag
You can do
你可以做
var status = $('input[type="checkbox"]').filter('.custom').map(function(){
return $(this).is(':checked') ? 1 : 0;
});
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.
然后status array
将以相同的顺序为每个复选框输入一个条目,0
用于未选中和1
已选中。它没有多大用处,因为您没有数组中复选框的其他信息。
If you want to perform operation based on the status
you can use .each()
instead, like
如果你想根据status
你可以使用来执行操作.each()
,比如
$('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
如果您想array of objects
使用name and status
复选框构建一个,您可以使用
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);?
Demo:http://jsfiddle.net/joycse06/rL3Ze/
演示:http : //jsfiddle.net/joycse06/rL3Ze/
回答by Pedro Casado
I have this code. I think you should adjust to your case.
我有这个代码。我认为你应该适应你的情况。
$('#formfriends input[type=checkbox]').each(function() {
if ($(this).attr('checked'))
//$(this).attr('disabled' , true);
// do what you want here
});
回答by ersanbilik
you can use jQuery each function http://api.jquery.com/jQuery.each/
你可以使用 jQuery 每个函数http://api.jquery.com/jQuery.each/
.each function will help you to traverse through your checkboxes
.each 函数将帮助您遍历复选框
回答by rémy
for jQm 1.3 i had to check for the class on the label in the wrapper div
对于 jQm 1.3,我必须检查包装器 div 中标签上的类
- ui-checkbox-off
- ui-checkbox-on
- ui-checkbox-off
- ui-checkbox-on
samplecode (already rendered by jQm):
示例代码(已由 jQm 呈现):
<div class="ui-checkbox">
<label data-corners="true" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="checkbox-off" data-theme="a" data-mini="false" class="ui-checkbox-off ui-btn ui-btn-corner-all ui-fullsize ui-btn-icon-left ui-btn-up-a">
<span class="ui-btn-inner">
<span class="ui-btn-text">
myLabel
</span>
<span class="ui-icon ui-icon-checkbox-off ui-icon-shadow"> </span>
</span>
</label>
<input type="checkbox" name="flip-num" class="flip-num">
</div>