Javascript 复选框值
我正在根据从后端获取的值动态创建复选框和相应的ID,如图所示。Javascript 复选框值,javascript,jquery,Javascript,Jquery,我正在根据从后端获取的值动态创建复选框和相应的ID,如图所示。 创建后,如何检索此复选框的值 HTML: <tr> <td class="valueleft">All</td> <td class="valueleft"><input type='checkbox' id="cb1"/></td> </tr> <tr> <td class="valueleft">----
创建后,如何检索此复选框的值 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>
$("#myTable").last().append("<tr><td>"+name+"</td><td><input type='checkbox'id="+id+"/></td></tr>");
全部的
--------
----复选框-------
jQuery:
<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>
$("#myTable").last().append("<tr><td>"+name+"</td><td><input type='checkbox'id="+id+"/></td></tr>");
$(“#myTable”).last().append(“+name+”);
要检索选中复选框的值,可以执行以下操作:
var checkedValues = [];
$('input[type=checkbox]:checked').each(function(){
//here this refers to the checkbox you are iterating on
checkedValues.push($(this).val());
});
或者,如果需要名称/值对,可以执行以下操作:
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
要检索选中复选框的值,可以执行以下操作:
var checkedValues = [];
$('input[type=checkbox]:checked').each(function(){
//here this refers to the checkbox you are iterating on
checkedValues.push($(this).val());
});
或者,如果需要名称/值对,可以执行以下操作:
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
您也可以使用。地图:
var checkedVals = $('input:checkbox:checked').map(function(){
return $(this).val();
}).get();
您也可以使用。地图:
var checkedVals = $('input:checkbox:checked').map(function(){
return $(this).val();
}).get();
你说“没用”是什么意思!你到底需要做什么?你说“它没用”是什么意思!你到底需要做什么?