开发者

checking boxes from a list of values

i have a list of values:

 <script>
 $().ready(function() {
var pi = '1,8';
    $('#items_'+i).attr("checked",true);
 });
 </script>                    

1 <input type="checkbox" id="items_1" name="myitems" value="1"  /> <br/>
2 <input type="checkbox" id="items_2" name="myitems" value="2"  /> <br/>
3 <input type="checkbox" id="items_3" name="myitems" value="3"  /> <br/>
4 <input type="checkbox" id="items_4" name="myitems" value="4"  /> <br/>
5 <input type="checkbox" id="items_5" name="myitems" value="5"  /> <br/>

6 <input type="checkbox" id="items_6" name="myitems" value="6"  /> <br/>
7 <input type="checkbox" id="items_7" name="myitems" value="7"  /> <br/>
8 <input type="checkbox" id="items_8" name="myitems" value="8"  /> <br/>
9 <input type="checkbox" id="items_9" name="myitems" value="9"  /> <br/>
开发者_开发百科

based on the above list pi, i want to only check items_1 and items_8. not sure how.


try that

var pi = '1,8';
var array = pi.split(','); // split on the comma creates an array
for(var x=0; x<array.length; x++){
    $('#items_'+x).attr("checked",true);}


Try this

$(function ()
{
    var pi = [1, 8];
    for (var i=0; i<pi.length; i++)
    {
        $('#items_' + pi[i]).attr('checked', true);
    }
});


Use the right form of jQuery's document.ready, and use a for loop.

$(function ()
{
    var pi = [1, 8]; // or '1,8'.split(',')
    for (var i=0; i<pi.length; i++)
    {
        $('#items_' + pi[i]).attr('checked', true);
    }
});


How about:

$(":checkbox:lt(8)").prop("checked", true);

Demo: http://jsfiddle.net/ELxPM/2/

Or:

$(":checkbox").slice(0,8).prop("checked", true);

// to narrow down to ids starting in items
$(":checkbox[id^='items']").slice(0,8).prop("checked", true);

// to apply your 'range' string
var range = pi.split(",");
$(":checkbox[id^='items']").slice(pi[0]-1, pi[1]-1).prop("checked", true);

Demo: http://jsfiddle.net/Y5xbd/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜