gpt4 book ai didi

javascript - 选中复选框值

转载 作者:行者123 更新时间:2023-11-28 21:19:34 25 4
gpt4 key购买 nike

我正在根据从后端获取的值动态创建复选框和相应的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com