简体   繁体   English

jQuery:选择$(this)的同级

[英]Jquery: Selecting Siblings of $(this)

I'm trying to get jquery to see when an element is changed and then run an action that involves it sibling like this: 我正在尝试让jquery查看何时更改了元素,然后运行涉及其兄弟姐妹的操作,如下所示:

$('.row').find('#class').change(function(){
        $(this).siblings('#teacher').removeAttr('disabled');
        var class_id = $(this).val();
    });

so when the input with an id of class is changed it will get its value and then affect its sibling with an id of teacher . 因此,当更改具有class ID的输入时,它将获得其值,然后通过teacher ID影响其同级。 They are both inside of <div class="row"> and this is repeated several times on the page. 它们都在<div class="row">内部,并且在页面上重复了几次。

The HTML: HTML:

<div class="row">
    <div class="grid_2">
        <select name="class" id="class">
        <option></option>
            //PHP that loads choices
        </select>
    </div>

        <div class="grid_2">
            <select name="teacher" id="teacher" disabled="true">
                 //Ajax loads choices
            </select>
        </div>
</div>

This is repeated Several times through out the page. 在整个页面中重复多次。

What I need help with is how to select the 2nd select box when the first one (next to it) changes. 我需要帮助的是如何在第一个(旁边的)更改时选择第二个选择框。

Thanks for the help. 谢谢您的帮助。

edit: When i try to console.log $(this).siblings() it comes back with [] as a result. 编辑:当我尝试console.log $(this).siblings()其返回结果为[]

While you should never have multiple elements with the same ID, if you do, you could select them with an attribute selector: 尽管您永远不应有多个具有相同ID的元素,但是如果有的话,可以使用属性选择器选择它们:

$(this).siblings('[id=teacher]');

For more info, see: Several elements with the same ID responding to one CSS ID selector 有关更多信息,请参见: 具有相同ID的多个元素响应一个CSS ID选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM