繁体   English   中英

勾选输入框时,JavaScript会选中复选框-复选框数组[]

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

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

希望这可以帮助。 问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM