选中返回并添加到字符串中的值



我试图在表中取一行复选框,对于表中的每个TD,它需要检查复选框是否被勾选,因此它是1,否则它是0。我需要一个for循环,在那里我将能够在每个td上循环,确定它是否被选中,如果是,则向二进制字符串添加1,或者向二进制字符串中添加0。

这应该以每个tr都有自己的唯一二进制字符串结束,该字符串来自已勾选或未勾选的复选框。我还想为每个tr设置自己唯一的ID。

下面的代码是我目前所拥有的,但我认为我走错了方向。

感谢您的帮助。感谢

[ { ID: 000, binary: 0101010101 }  ]
function generate(){

$("#actionTable>tbody").children("tr").each(function(i, rowitem){
$(rowitem).children("td").each(function(index, item){
if (index == 0){
var amid = $(item).data("amid");
}
else {
//Handle checkbox
var checked = $(item.firstChild).prop('checked')
}
});
});

}

您可以为此使用嵌套的map((。外部映射迭代行

内部映射迭代每个输入的checked属性,该属性先转换为数字,然后转换为字符串,然后加入数组作为最终字符串。

我不知道html是什么样子的,所以现在只使用了每行的行索引

const res = $('tr').map((i, el) => {
const binary = $(el).find(':checkbox').map((_, inp) => {
return `${+inp.checked}`;
}).get().join('');

return { index: i, binary};

}).get()
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="checkbox"></td>
<td><input type="checkbox" checked></td>
<td><input type="checkbox"></td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td><input type="checkbox" checked></td>
<td><input type="checkbox"></td>
<td><input type="checkbox" checked></td>
<td><input type="checkbox"></td>
</tr>
</table>

最新更新