Jquery 选择器 - 如何选择下一列文本区域

发布于 2025-01-07 02:20:39 字数 598 浏览 2 评论 0原文

好的,

这是示例:

<table>
    <tr>
        <td>
            <type="button" id="mybutton" value="insert">
        </td>
        <td>
            <textarea>My Text</textarea>
        </td>
    </tr>
</table>

当我单击按钮时,我需要更改文本区域值。该值将来自设置变量。此代码没有其他 ID 或类可供引用。所以我有以下内容

jQuery('#mybutton').click(function(){

  jQuery(this).parent().next()...????;

});

这将使我进入下一栏,但我不知道如何从那里选择 textarea

我尝试了 child() 和另一个 next() 但没有成功。

Ok,

Here's the exampe:

<table>
    <tr>
        <td>
            <type="button" id="mybutton" value="insert">
        </td>
        <td>
            <textarea>My Text</textarea>
        </td>
    </tr>
</table>

I need to change the textarea value when I click on my button. The value will come from a set variable. This code has no other IDs or classes to refer to. So I have the following

jQuery('#mybutton').click(function(){

  jQuery(this).parent().next()...????;

});

This will get me to the next column, but I don't know how to select the textarea from there.

I tried child(), another next() but no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

极致的悲 2025-01-14 02:20:39

要在 jQuery 中选择子元素,我们使用 .children()

jQuery('#mybutton').click(function(){
    jQuery(this).parent().next().children().val(some_variable);
});

请注意,这也会选择 textarea 的所有同级元素,除非您添加 textarea > 作为 .children() 函数调用中的选择器: .children('textarea')

您还可以使用 .closest().find()

jQuery('#mybutton').click(function(){
    jQuery(this).closest('tr').find('textarea').val(some_variable);
});

.closest() 查找与选择器匹配的第一个祖先元素,然后查找,很好地在根选择的后代元素中找到选择器。

给你的一些文档:

To select child elements in jQuery we use .children():

jQuery('#mybutton').click(function(){
    jQuery(this).parent().next().children().val(some_variable);
});

Note that this will select all sibling elements to the textarea as well unless you add textarea as a selector in the .children() function call: .children('textarea')

You could also use .closest() and .find():

jQuery('#mybutton').click(function(){
    jQuery(this).closest('tr').find('textarea').val(some_variable);
});

.closest() finds the first ancestor element that matches the selector, and find, well finds the selector in the descendant elements of the root-selection.

Some Documentation for ya:

江城子 2025-01-14 02:20:39

如果此表将来可能会发生变化,您不希望如此含糊。这是您想要的更具体的内容:

$(this).closest('table').find('textarea').val('your val');

更多信息:

.closest ()
.find()

If this table might change in the future you don't want to be that ambiguous. Here is something that is a little more concrete in what you want:

$(this).closest('table').find('textarea').val('your val');

More info:

.closest()
.find()

别把无礼当个性 2025-01-14 02:20:39

尝试使用这样的东西:

jQuery(this).parent().next().find('textarea').val('CHANGE VALUE');;

Try using something like this:

jQuery(this).parent().next().find('textarea').val('CHANGE VALUE');;
听,心雨的声音 2025-01-14 02:20:39

有很多方法可以实现同样的目标。以下为一

$(this).parent().siblings().find('textarea').val("New Text");

There are lot of ways to achieve the same thing. The following is one

$(this).parent().siblings().find('textarea').val("New Text");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文