我有一个简单的 JavaScript 问题。我有一个复选框和一个输入框。
当用户在输入框(onkeydown)中输入文本时,我希望它检查该行中相应的复选框。
我的复选框都共享相同的数组名称 checkbox[]
。
我的简化语法是:
<table>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
</td>
<tr>
<?php endforeach ; ?>
</table>
由于复选框的名称不是唯一的,只有其值,我如何告诉 JavaScript 要检查哪个复选框?
<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
一如既往地感谢帮助。
谢谢
最佳答案
HTML
<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name1" id="name1" />
<br>
<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name2" id="name2" />
<br>
<input type="checkbox" name="checkbox[]" />
<input type="text" name="name3" id="name3" />
Jquery
$(':text').change(function(){
$(this).prev(':checkbox').attr('checked','true');
});
希望这有帮助。 问候。
关于javascript 在输入框被勾选时检查复选框 - 复选框数组 [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199670/