作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在根据从后端获取的值动态创建复选框和相应的 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>");
最佳答案
要检索已检查的 checkobxes 的值,您可以执行以下操作:
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
关于javascript - 选中复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6743337/
我是一名优秀的程序员,十分优秀!