检查是否选中了JQuery移动复选框 [英] Check if a JQuery mobile checkbox is checked

查看:134
本文介绍了检查是否选中了JQuery移动复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框和列表的列表。我想得到每个人的状态。
列表在这里:

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/

详细了解 .each() .map()

Read more on .each() and .map()

这篇关于检查是否选中了JQuery移动复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆