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

javascript - Get the rel attribute value of all checked inputs as an array or csv using jQuery - Stack Overflow

programmeradmin0浏览0评论

I have around 9 checkboxes with different rel values. How can I get the all the rel values of checked check boxes in an array or as csv using jQuery?

<input type="checkbox" name="item" rel="WVSEUPU" />

I have around 9 checkboxes with different rel values. How can I get the all the rel values of checked check boxes in an array or as csv using jQuery?

<input type="checkbox" name="item" rel="WVSEUPU" />
Share Improve this question edited Feb 12, 2017 at 20:05 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jul 18, 2011 at 8:37 esafwanesafwan 18.1k35 gold badges110 silver badges170 bronze badges 1
  • 2 input elements don't have a rel attribute, either use a standard attribute (e.g. class) or an HTML5 data- attribute. – RobG Commented Jul 18, 2011 at 9:27
Add a ment  | 

2 Answers 2

Reset to default 4
var relArr=[];
$("input[type='checkbox']:checked").each(function(){
relArr.push($(this).attr('rel'));
});

here is the working fiddle http://jsfiddle/7zSRQ/

You could do:

var rels = [];

$('input[type="checkbox"]:checked').each(function(){
    var rel = $(this).attr('rel');
    rels.push(rel);
});
//rels is an array you could use rels.join(',') to join the array.
发布评论

评论列表(0)

  1. 暂无评论