javascript - create string from all checked values jQuery - Stack Overflow

Ok so I do this:<input type="checkbox" class="checkBoxClass" value="0"

Ok so I do this:

<input type="checkbox" class="checkBoxClass" value="0" />
<input type="checkbox" class="checkBoxClass" value="1" />
<input type="checkbox" class="checkBoxClass" value="2" />

Then in javascript/jquery I'm trying to do something like this:

$('#btnHtml').click(function(){
for( var checks=''; $('.checkBoxClass:checked').val() ){
    var checks = checks+', ';
}
});

Let's say all of those checkboxes are checked, how can I get var checks to be a string of '0, 1, 2' ?

Still trying to get used to the for() in JS, it might be the pletely wrong way of going about it. Not sure how to do this.

Ok so I do this:

<input type="checkbox" class="checkBoxClass" value="0" />
<input type="checkbox" class="checkBoxClass" value="1" />
<input type="checkbox" class="checkBoxClass" value="2" />

Then in javascript/jquery I'm trying to do something like this:

$('#btnHtml').click(function(){
for( var checks=''; $('.checkBoxClass:checked').val() ){
    var checks = checks+', ';
}
});

Let's say all of those checkboxes are checked, how can I get var checks to be a string of '0, 1, 2' ?

Still trying to get used to the for() in JS, it might be the pletely wrong way of going about it. Not sure how to do this.

Share Improve this question asked Jan 29, 2013 at 15:20 SilasSilas 5822 gold badges10 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

Functional programming:

var str = $('.checkBoxClass:checked').map(function() {
    return this.value;
}).get().join();

Reference: .map, .get, .join

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743713982a4494668.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信