简体   繁体   English

jQuery:选择在另一个指定元素之后发生的元素的第一个实例

[英]jQuery: selecting the first instance of an element that occurs after another specified element

Using this as a simplified example, assume I have a table that has some radio buttons in it followed by a div element with a link in it. 使用它作为一个简化的例子,假设我有一个表中有一些单选按钮,后跟一个带有链接的div元素。 This pattern is then repeated some unknown number of times like so: 然后重复这种模式一些未知的次数,如下所示:

<table class="rdoBtnList">
    <tbody>
       <tr>
          <td> Person 1 </td>
          <td>
            <label for="rb1">Verified</label>
            <input type="radio" id="rb1" name="rdoBtns" value="Verified" />
            <label for="rb2">Not Verified</label>
            <input type="radio" id="rb2" name="rdoBtns" value="NotVerified" />
          </td>
       </tr>
    </tbody>
 </table>
 <div class="PersonLink"><a href="#">Link to Person 1 page</a></div>
  ..... tables and divs for person 2, 3, ... n, etc

I would like to be able to use jQuery to enable/disable the link in the div following the radio buttons based on the value of the radio button. 我希望能够使用jQuery根据单选按钮的值启用/禁用单选按钮后面的div中的链接。 I can get the value of the radio buttons, but cannot figure out what selector syntax I would use to enable/disable just the div after the radio buttons. 我可以得到单选按钮的值,但无法弄清楚什么选择语法我会用启用/单选按钮后禁用股利。

Here is my jQuery so far: 到目前为止,这是我的jQuery:

    <script type="text/javascript">
       $(document).ready(function() {
           $(".rdoBtnList > tbody > tr > td > input").change(function() {
           if ($(this).val() == "Verified") {
               // select the link in the div following the table and enable it
           } else {
               // select the link in the div following the table and disable it
           }
        });
       });
    </script>

Like this: 像这样:

$(this).closest('.rdoBtnList').nextAll('.PersonLink:first').children('a').whatever

closest('.rdoBtnList') will find the table, and nextAll('.PersonLink:first') will find the first .PersonLink after the table. closest('.rdoBtnList')将找到该表,而nextAll('.PersonLink:first')将在表之后找到第一个.PersonLink

By the way, the val function returns a normal boolean. 顺便说一句, val函数返回一个普通的布尔值。 You should write if($(this).val()) . 你应该写if($(this).val())

I would have tried a different approach, giving each link an id, or perhaps even a class for easier selection: 我会尝试一种不同的方法,给每个链接一个id,或者甚至是一个类,以便于选择:

<table>...
    <input type="radio" id="personXrb1" class="personXselector" ...
<div>
<a href="personXlink" id="personXselector">link to person X</a>

This should be relatively easy as I expect the html is generated programatically. 这应该相对容易,因为我希望以编程方式生成html。 Right? 对? It will also be less prone to errors due to code changes, and possibly easier to read. 由于代码更改,它也不太容易出错,并且可能更容易阅读。

Selection would then not be done with $(this).closest etc, which is not guaranteed to work after changing the code around, but instead through 然后不会使用$(this).closest等进行选择,这在改变代码后无法保证工作,而是通过

$('#' + $(this).attr('class')).doStuff();

I'm also not sure that enable/disable will work with a div or a-tag, you might be better off using fadeOut() and fadeIn() or just show() and hide() to "disable" the links. 我也不确定启用/禁用是否可以使用div或a-tag,你可能最好使用fadeOut()和fadeIn()或只是show()和hide()来“禁用”链接。

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

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