2017-02-24 77 views
1

當我點擊某行的單選按鈕(<tr>)時,有沒有設置textarea的方法?Javascript Radio add value Textarea

function changeTeks(i){ 
 
    var teks = ''; 
 
    var row = $(this).parents('tr'); 
 
    if(i == '1'){ 
 
    teks = row.find('input[name="brazil[]"]').val(); 
 
    }else if(i == '2'){ 
 
    teks = row.find('input[name="normal[]"]').val(); 
 
    }else if(i == '3'){ 
 
    teks = row.find('input[name="gagal[]"]').val(); 
 
    }else{ 
 
    teks = "belum"; 
 
    } 
 
    row.find('textarea[name="hasil[]"]').text(teks); 
 
    console.log(teks); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table border="1"> 
 
<tr> 
 
<td>1</td> 
 
<input type='hidden' name='gagal[]' value='fail in test 1' /> 
 
<input type='hidden' name='normal[]' value='not yet in test 1 ' /> 
 
<input type='hidden' name='brazil[]' value='success in test 1' /> 
 
<td><input type='hidden' name='lingkup[]' value='30' />Test 1</td> 
 
<td><input type='radio' name='target0[]' value='1' onclick='changeTeks(1)' /> 
 
</td> 
 
<td><input type='radio' name='target0[]' value='2' onclick='changeTeks(2)' /> 
 
</td> 
 
<td><input type='radio' name='target0[]' value='3' onclick='changeTeks(3)' /> 
 
</td> 
 
<td><textarea name='hasil[]' class='form-control'></textarea></td> 
 
</tr> 
 
<tr> 
 
<td>1</td> 
 
<input type='hidden' name='gagal[]' value='fail in test 2' /> 
 
<input type='hidden' name='normal[]' value='not yet in test 2' /> 
 
<input type='hidden' name='brazil[]' value='success in test 2' /> 
 
<td><input type='hidden' name='lingkup[]' value='30' />Test 2</td> 
 
<td><input type='radio' name='target1[]' value='1' onclick='changeTeks(1)' /> 
 
</td> 
 
<td><input type='radio' name='target1[]' value='2' onclick='changeTeks(2)' /> 
 
</td> 
 
<td><input type='radio' name='target1[]' value='3' onclick='changeTeks(3)' /> 
 
</td> 
 
<td><textarea name='hasil[]' class='form-control'></textarea></td> 
 
</tr> 
 
<tr> 
 
<td>1</td> 
 
<input type='hidden' name='gagal[]' value='fail in test 3' /> 
 
<input type='hidden' name='normal[]' value='not yet in test 3' /> 
 
<input type='hidden' name='brazil[]' value='success in test 3' /> 
 
<td><input type='hidden' name='lingkup[]' value='30' />Test 3</td> 
 
<td><input type='radio' name='target2[]' value='1' onclick='changeTeks(1)' /> 
 
</td> 
 
<td><input type='radio' name='target2[]' value='2' onclick='changeTeks(2)' /> 
 
</td> 
 
<td><input type='radio' name='target2[]' value='3' onclick='changeTeks(3)' /> 
 
</td> 
 
<td><textarea name='hasil[]' class='form-control'></textarea></td> 
 
</tr> 
 
           </table>

我總是從父undefined值。我想填寫textarea,每行input type hidden value(因爲這個隱藏值對於每一行不同)

+1

功能'this'不指元件內部,通過參考作爲參數 –

+0

所以我需要改變'this'與? –

+0

'changeTeks(1)'==>'changeTeks(1,this)'和'function changeTeks(i)var teks =''; (var)= 0(var)=(this).parents('tr');'==>'function changeTeks(i,ele)var teks =''; var row = $(ele).parents('tr');' –

回答

2

從Pranav的評論通this在你的函數中,並在裏面使用它。

function changeTeks(ele,i){ 
 
\t var teks = ''; 
 
\t var row = $(ele).closest('tr'); 
 
\t if(i == '1'){ 
 
\t  teks = row.find('input[name="brazil[]"]').val(); 
 
\t }else if(i == '2'){ 
 
\t  teks = row.find('input[name="normal[]"]').val(); 
 
\t }else if(i == '3'){ 
 
\t  teks = row.find('input[name="gagal[]"]').val(); 
 
\t }else{ 
 
\t  teks = "belum"; 
 
\t } 
 
\t row.find('textarea[name="hasil[]"]').html(teks); 
 
\t console.log(teks); 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table border="1"> 
 
<tr> 
 
<td>1</td> 
 
<input type='hidden' name='gagal[]' value='fail in test 1' /> 
 
<input type='hidden' name='normal[]' value='not yet in test 1 ' /> 
 
<input type='hidden' name='brazil[]' value='success in test 1' /> 
 
<td><input type='hidden' name='lingkup[]' value='30' />Test 1</td> 
 
<td><input type='radio' name='target0[]' value='1' onclick='changeTeks(this,1)' /> 
 
</td> 
 
<td><input type='radio' name='target0[]' value='2' onclick='changeTeks(this,2)' /> 
 
</td> 
 
<td><input type='radio' name='target0[]' value='3' onclick='changeTeks(this,3)' /> 
 
</td> 
 
<td><textarea name='hasil[]' class='form-control'></textarea></td> 
 
</tr> 
 
<tr> 
 
<td>1</td> 
 
<input type='hidden' name='gagal[]' value='fail in test 2' /> 
 
<input type='hidden' name='normal[]' value='not yet in test 2' /> 
 
<input type='hidden' name='brazil[]' value='success in test 2' /> 
 
<td><input type='hidden' name='lingkup[]' value='30' />Test 2</td> 
 
<td><input type='radio' name='target1[]' value='1' onclick='changeTeks(this,1)' /> 
 
</td> 
 
<td><input type='radio' name='target1[]' value='2' onclick='changeTeks(this,2)' /> 
 
</td> 
 
<td><input type='radio' name='target1[]' value='3' onclick='changeTeks(this,3)' /> 
 
</td> 
 
<td><textarea name='hasil[]' class='form-control'></textarea></td> 
 
</tr> 
 
<tr> 
 
<td>1</td> 
 
<input type='hidden' name='gagal[]' value='fail in test 3' /> 
 
<input type='hidden' name='normal[]' value='not yet in test 3' /> 
 
<input type='hidden' name='brazil[]' value='success in test 3' /> 
 
<td><input type='hidden' name='lingkup[]' value='30' />Test 3</td> 
 
<td><input type='radio' name='target2[]' value='1' onclick='changeTeks(this,1)' /> 
 
</td> 
 
<td><input type='radio' name='target2[]' value='2' onclick='changeTeks(this,2)' /> 
 
</td> 
 
<td><input type='radio' name='target2[]' value='3' onclick='changeTeks(this,3)' /> 
 
</td> 
 
<td><textarea name='hasil[]' class='form-control'></textarea></td> 
 
</tr> 
 
           </table>

+0

非常感謝你,它現在的作品.. –