开发者

How can I check check-boxes according to value using jQuery?

How can I check check-boxes (on page load) according to value of check-boxes using jQuery? I have list of values that I have stored within localstorage (Long live HTML5 !!) A sample data would be

something_random|something_random2|something_random3

And basically I want to read each value开发者_运维技巧 and make sure onload checkboxes that already have those values are checked.

I have already written;

my_cookie=localStorage.getItem('chosen_ones');

      $.each(my_cookie.split("|"), function(index, values) {
               if((values==val)&&(values!=null)&&(values!="")&&(values!="null")&&(values!="")){
                 $('input[value='+values+'').attr('checked', true);
            }
        });

However this didn't work. How can I fix it?


I would suggest something along the lines of:

$('input[type=checkbox][value='+values+']').attr('checked', true);

Notice the type, and the closing ']'.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜