2011-07-19 171 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();