如何遍历具有相同类名的复选框组? [英] How to loop through group of check boxes that have same class name?
问题描述
我有带复选框字段的表格单元格的动态表格。这些字段从数据库填充,所以我的表是动态的。我想通过基于类名的复选框进行循环。在那个循环中,我想检查每个复选框的值。例如,如果值等于1我希望复选框被检查,如果不是未选中。 Alo我不确定是否有可能,但我想为每个复选框设置唯一的ID。由于我的表格是动态的,我的字段需要具有唯一的ID。以下是我的代码的示例:
I have dynamic table that has table cell with checkbox field. This fields are populated from DB so my table is dynamic. I would like to loop through checkboxes based on their class name. In that loop I want to check value for each check box. For example if value is equal 1 I want checkbox to be checked, if not unchecked. Alo I'm not sure if possible but I would like to set unique ID for each of these check boxes. Since my table is dynamic my fields need to have unique ID's. Here is example of my code:
<table>
<thead>
<tr>
<th>#</th>
<th>Time Slots</th>
<th>Block</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>~(SLOT_LABEL)</td>
<td>
<span>
<input type="checkbox" name="CF-[Events]" class="block" id="block_"+Value that will make this ID unique. value="~(SLOT_ID)"/>
</span>
</td>
</tr>
</tbody>
</table>
另外在使用当前语言来读取数据库中的值时,我必须使用NAME标记。如果有人能帮助请让我知道。谢谢。
Also in current language that I work with to read values from DB I have to use NAME tag. If anyone can help please let me know. Thank you.
推荐答案
您可以使用属性选择器以名称
和值
。试试这个:
You can use the attribute selector to retrieve elements by both their name
and value
. Try this:
$('input[name="CF-[Events]"][value="1"]').prop("checked", true);
这篇关于如何遍历具有相同类名的复选框组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!