2011-07-19 170 views
0

根据我从后端得到的值,我为它们动态创建复选框和相应的ID。
创建完成后,如何检索此复选框的值?已勾选复选框值

HTML:

<tr> 
    <td class="valueleft">All</td> 
    <td class="valueleft"><input type='checkbox' id="cb1"/></td> 
</tr> 
<tr> 
    <td class="valueleft">--------</td> 
    <td class="valueleft">----checkbox-------</td> 
</tr> 

的jQuery:

$("#myTable").last().append("<tr><td>"+name+"</td><td><input type='checkbox'id="+id+"/></td></tr>"); 
+2

http://stackoverflow.com/questions/786142/how-to-retrieve-checkboxes-values-in-jquery – Binil

回答

3

要检索核对checkobxes你可以做类似的值:

var checkedValues = []; 

$('input[type=checkbox]:checked').each(function(){ 
     //here this refers to the checkbox you are iterating on 
     checkedValues.push($(this).val()); 
}); 

,或者如果您希望名称/值对你c乌尔德做:

var checkedValues = {}; 

$('input[type=checkbox]:checked').each(function(){ 
     //here this refers to the checkbox you are iterating on 
     checkedValues[$(this).attr('id')] = $(this).val(); 
}); 

//you end up with an object with the id's as properties and the relative values as values 
+0

它没有工作伙伴。 – Suga

+0

你的意思是“它没有工作”!你需要做些什么? –

0

您还可以使用.MAP:

var checkedVals = $('input:checkbox:checked').map(function(){ 
    return $(this).val(); 
}).get();