Jim*_*Jim 5 html javascript string jquery traversal
我在html表的列中有一个复选框.当我选中或取消选中它时,我希望在同一行的下一列的文本区域中显示/删除一些文本.
我做了以下事情:
$(this).parent().parent().find('textarea').text = 'some text'
Run Code Online (Sandbox Code Playgroud)
并且
$(this).parent().parent().find('textarea').val = 'some text'
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
html就像:
<tr>
<td>
<input type="checkbox" />
</td>
<td>
<textarea>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我想得到我检查的复选框的相同tr的textarea
UPDATE
我发现我应该使用.val("some text")但现在仅当我单击第一行中的复选框时才调用该函数.不是剩下的
Mar*_*ers 18
问题在于您如何设置值而不是如何查找元素
试试这个
$(this).closest('tr').find('textarea').val("some text");
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参见此处 VAL()
UPDATE
元素ID必须是唯一的,因此您不能重复使用相同的元素.给你所有的复选框都是唯一的id,即"chkOne","chkTwo"等.然后在你希望运行此功能的所有复选框上使用一个类.即class="chkSelection".然后将您的jQuery更改为这样
$('.chkSelection').change(function() {
if($(this).is(':checked')){
$(this).closest('tr').find('textarea:first').text('Some text here');
}
});
Run Code Online (Sandbox Code Playgroud)
这样,所有带有"chkSelection"类的复选框在更改时将运行功能以查找下一个textarea并设置文本.