开发者

Is it possible to Improve this Jquery code?

I am currently doing this:

var currFilterList = "";
$('.prod-filters input[type=checkbox]:checked').each(function() { 
currFilterList += $(this).attr('data-groupid')+',';
});

I was thinking it could be cool to do something like:

var currFilterList = $(blahblah).each(function() { return += value; }

The other thing I wanted to do was to add an item to an arra开发者_Python百科y of JQUERY objects (which needed to be null initially, I tried merge, but doesn't work as its not an array of JQUERY objects, is there a way to do this?

var myArrayOfJqueryObjects = [];
foreach(blah) {
$('.myItem').ADDTO(myArrayOfJqueryObjects );
}

Thanks alot for any replies :)!!!


You can write

var arr = $('.prod-filters input:checkbox:checked').map(function() { 
    return $(this).attr('data-groupid'); 
}).get();

var str = arr.join(',');


Not sure this would work, but worth a try:

var currFilterList = $('.prod-filters input[type=checkbox]:checked').map($(this).attr('data-groupid')).join(',');


This may be considered improved...

var currFilterList = [];
$('.prod-filters input:checkbox:checked').each(function() { 
   currFilterList.push($(this).data('group-id'));
});

currFilterList = currFilterList.join(',');

Though the code will produce different output, I assumed you did not want a trailing ,.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜