开发者

Using jQuery to iterate across like named checkboxes

开发者 https://www.devze.com 2022-12-18 03:09 出处:网络
I am trying to use jQuery each() to iterate across a group of checkboxes named supplier_type[].They all have unique ids so I could just re-write this to handle an array of unique ids but I wanted to f

I am trying to use jQuery each() to iterate across a group of checkboxes named supplier_type[]. They all have unique ids so I could just re-write this to handle an array of unique ids but I wanted to figure out where I went wrong here.

The alert shows that while intIndex sees the correct number of elements this always holds the first element.

function setUp(){
    $( 'input[name=supplier_type[]]' ).each(
        function( intIndex ){
            var type = $(this+':checked').val() + 'Table';
            $("#"+type).show("slow");
            alert($(this+':checked').val());  // sees first one each iter
        }       
    );
}

So how do I address the correct element? Do I need to use bind()?

Thanks!

// Edit Working version based on Drew Wills response. Thanks Drew!

function setUp(){
    $( 'input[name=supplier_type[]]' ).each(
        function( intIndex ){
            if($(this).attr('checked')) {
                var type = $(this).val() + 'Table';
                $("#"+type).show("slow");
            }
        }       
    );开发者_运维知识库
}


I'm not sure I understand your question 100%, but I'm very suspicious of the following style of selector:

$(this+':checked')

I'm guessing you want to know the value of the 'checked' attribute, which would be more like...

$(this).attr('checked')
0

精彩评论

暂无评论...
验证码 换一张
取 消