2013-03-04 96 views
1

我有一個簡單的JavaScript問題。我有一個複選框和一個輸入框。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;"> 

幫助一如既往的讚賞。

感謝

+0

通話的onkeydown /平變化與通過這個作爲一個參數,然後在功能使用 '功能ABC(ELEM) {$ (本).parent()的next()找到('的方法:。複選框').attr(' 檢查」,假); }' – 2013-03-04 10:49:34

+0

你可以通過看小提琴來協助嗎? http://jsfiddle.net/A4GaF/1/ – Smudger 2013-03-04 11:14:32

回答

1

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'); 
}); 

希望這有助於。 此致敬禮。

+0

Hi X因子。謝謝你的時間。不像每個小提琴一樣工作,http://jsfiddle.net/A4GaF/1/。任何想法爲什麼?腳本如何知道它必須找到哪個複選框。所以在小提琴上,如果我輸入任何框,它應該檢查相應的複選框。謝謝 – Smudger 2013-03-04 11:14:17

+0

好吧,讓我有一個loook – 2013-03-04 11:30:26

+0

我更新了你的代碼,但你必須添加jQuery爲該@Smudger – 2013-03-04 11:50:49