2017-02-24 47 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

非常感谢你,它现在的作品.. –