我有一个从数据库查询生成的表单。生成的字段是数组name='monitor[]'
和id='monitor[]'
等的一部分。
如果要勾选另一个复选框,我想让jquery选中一个复选框。
这是从数据库查询中生成的一些形式...
<td><input type='checkbox' id='monitor[$i]' name='monitor[$i]' value='1' /></td>
<td><input type='checkbox' id='priority[$i]' name='priority[$i]' value='1' onclick='selectMon()'/></td>
<td><select name='position[$i]' onchange='selectMon()'>
<option selected='selected' disabled='true'></option>
<option value='support'>Support</option>
<option value='oppose'>Oppose</option>
<option value='watch'>Watch</option>
</select></td>
请注意onclick事件。
jq ...
function selectMon() {
if ($('input[name="priority[]"]').prop('checked')) {
$('input[name="monitor[]"]').attr('checked', true);
}
}
我尝试了jq的不同变体。我在方括号之前尝试过\ \。我尝试定义“ i”(数组编号)。我搜索了过去几个小时。
我正在尝试做到这一点,以便如果选中了优先级,监视器也将被检查。另外,如果选择了位置,还将检查监视器。
如果不是数组[](字段的多次迭代),这似乎很简单。
我想念什么?
答案 0 :(得分:1)
您需要在其中传递索引
onclick='selectMon($i)'
function selectMon(index) {
if ($('input[name="priority[' + index + ']"]').prop('checked')) {
$('input[name="monitor[' + index + ']"]').attr('checked', true);
}
}