最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to checkuncheck checkbox by using value in query - Stack Overflow

programmeradmin0浏览0评论

HTML CODE:

<form id="myform">
 <input type='checkbox' name='foo[]' value='1'>
 <input type='checkbox' name='foo[]' checked='true' value='2' >
 <input type='checkbox' name='foo[]' value='3' >
 <input type='checkbox' name='foo[]' checked='true' value='4' >
</form>

JS CODE:

$('input[name=foo]:checked').each(function(i,el){
    if(clusterVal == 'ALL')
        {
            /* what do I have to do here? */
        }
    array.push($(el).val());
});

how to check/uncheck checkbox by using value in query

HTML CODE:

<form id="myform">
 <input type='checkbox' name='foo[]' value='1'>
 <input type='checkbox' name='foo[]' checked='true' value='2' >
 <input type='checkbox' name='foo[]' value='3' >
 <input type='checkbox' name='foo[]' checked='true' value='4' >
</form>

JS CODE:

$('input[name=foo]:checked').each(function(i,el){
    if(clusterVal == 'ALL')
        {
            /* what do I have to do here? */
        }
    array.push($(el).val());
});

how to check/uncheck checkbox by using value in query

Share Improve this question edited Aug 3, 2012 at 11:17 Zeta 106k13 gold badges204 silver badges246 bronze badges asked Aug 3, 2012 at 11:14 Vishakha SehgalVishakha Sehgal 331 silver badge5 bronze badges 1
  • Did you google it ?! google..lb/… – amd Commented Aug 3, 2012 at 11:16
Add a ment  | 

5 Answers 5

Reset to default 3

As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

try this ..but this to just demo how to do check and uncheck

DEMO

 $(function(){
        var el=$('input:checkbox[name="foo[]:checked"]');

        el.each(function()
          {
                         $(this).prop('checked', true);
          });
    });

Set the checked attribute of the checkbox element to true or false, i.e.

el.checked = false;

If you want to do it with jquery, use the attr function an the checked attibute as well

Hans is right, although el will likely be a jQuery object (.each method returns jQuery objects in this case. as Anthony pointed out, This isn't always the case.)But as Anthony pointed out to me, el won't always be a pure dom object. It can sometimes be a jQuery object, so the checked property isn't necessarily available. A couple of ways to deal with those cases:

$(el).get(0).checked = false;
$(el).removeAttr('checked');//or $(el).attr('checked',false);

$(el).get(0).checked = true;
$(el).attr('checked','checked');

Credit to Anthony for pointing out my thick-headedness on $.each and .each's behaviour :)

Use the prop or the attr to do this. Something like this

$(el).attr("checked", "checked");

or

$(el).prop("checked", true);

Jquery API Prop()

You can check or uncheck element following

$(element).prop('checked',true); // check
$(element).prop('checked',false); // uncheck
发布评论

评论列表(0)

  1. 暂无评论