簡體   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