Javascript在输入框被选中时检查复选框- checkbox array[]

javascript to check checkbox when input box is ticked - checkbox array []

本文关键字:复选框 checkbox array 检查 输入 Javascript      更新时间:2023-09-26

我有一个简单的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');
});

希望这对你有帮助。问候。