2013-04-08 61 views
0

我想获取一组复选框的ID的字符串。下面的代码确实包含这些ID,但它也包含空格和双逗号,用于未选中的复选框。获取复选框ID列表

有没有办法获得一个只有ID的字符串?

谢谢!

$($('input[type=checkbox][name=selector]')).each(function() { 
     var sThisVal = (this.checked ? this.id : ""); 
     sList += (sList == "" ? sThisVal : "," + sThisVal); 
}); 

回答

5

您可以使用map()获得逗号分隔选中的复选框

strIds = $('input[type=checkbox][name=selector]').map(function() {       
     if(this.checked) return this.id;  
}).get().join(','); 

使其成为通过简化选择,使选择使用:checked selector返回上选中的复选框一点简单的IDS。

strIds = $('[name=selector]:checked').map(function() {       
     return this.id;  
}).get().join(','); 
+0

我有多个复选框,所以我需要通过所有的人使用。每循环 - 感谢 – SkyeBoniwell 2013-04-08 16:25:20