【问题标题】:.next(".myClass") Not Working Jquery.next(".myClass") 不工作 Jquery
【发布时间】:2014-12-15 09:57:03
【问题描述】:

我不知道如何获得我想要的价值我尝试了很多东西,但我不知道如何找到我的价值。 这是我的 HTML:

<table class="liste">
    <tbody>
        <tr>
            <th>Actions</th>
            <th>Nom</th>
            <th>Coordonnées</th>
            <th>Diplômes</th>
            <th>Profil associé</th>
        </tr>
        <tr>
            <td class="groupe" colspan="5">2014-2015            
                <button class="copyMails" title="Copier les adresses emails">
                     <img src="/images/picto/action_dupliquer.png" alt="">
                    Copier les adresses emails
                </button>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" class="lstMails" value="myText">
            </td>
        </tr>
        <tr>
            <th>Actions</th>
            <th>Nom</th>
            <th>Coordonnées</th>
            <th>Diplômes</th>
            <th>Profil associé</th>
        </tr>
        <tr>
            <td class="groupe" colspan="5">2014-2015            
                <button class="copyMails" title="Copier les adresses emails">
                     <img src="/images/picto/action_dupliquer.png" alt="">
                    Copier les adresses emails
                </button>
            </td>
        </tr>
        <tr>
            BLABLABLA
        </tr>
        <tr>
            BLABLABLA
        </tr>
        <tr>
            <td>
                <input type="text" class="lstMails" value="myText2">
            </td>
        </tr>
    </tbody>
</table>

这是我的 JS:

$("body").on('click', ".copyMails", function() {
    console.log($(this).parent().parent().parent().next('.lstMails').val());
});

我已经尝试过和父母一起做更多和更少等等。但我不知道该怎么做。 当我点击按钮 .copyMails 时,我想访问下一个 .lstMails

【问题讨论】:

  • 为它制作小提琴

标签: javascript jquery next


【解决方案1】:

您应该使用closest() 导航父tr 然后使用next() 指向下一个tr 然后您可以使用find()

使用

$("body").on('click', ".copyMails",function(){
    console.log($(this).closest('tr').next('tr').find('.lstMails').val());
});

更新

$("body").on('click', ".copyMails", function() {
    alert($(this).closest('tr').nextAll('tr:has(.lstMails)').find('.lstMails').val());
});

【讨论】:

  • 感谢您的回答。问题是 .lstMails 并不总是在下一个 tr 中,我已经编辑了我的 html 以显示一个示例。
  • @Meezy,更新答案
  • 非常感谢您的回答。完美工作
【解决方案2】:

您需要找到下一个tr 同级元素,然后是其中的lstMails 元素

$("body").on('click', ".copyMails", function() {
  $('#log').html($(this).closest('tr').nextAll('tr:has(.lstMails)').first().find('.lstMails').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="liste">
  <tbody>
    <tr>
      <th>Actions</th>
      <th>Nom</th>
      <th>Coordonnées</th>
      <th>Diplômes</th>
      <th>Profil associé</th>
    </tr>
    <tr>
      <td class="groupe" colspan="5">2014-2015
        <button class="copyMails" title="Copier les adresses emails">
          <img src="/images/picto/action_dupliquer.png" alt="">Copier les adresses emails
        </button>
      </td>
    </tr>
    <tr>
      <td>
        <input type="text" class="lstMails" value="myText">
      </td>
    </tr>
    <tr>
      <th>Actions</th>
      <th>Nom</th>
      <th>Coordonnées</th>
      <th>Diplômes</th>
      <th>Profil associé</th>
    </tr>
    <tr>
      <td class="groupe" colspan="5">2014-2015
        <button class="copyMails" title="Copier les adresses emails">
          <img src="/images/picto/action_dupliquer.png" alt="">Copier les adresses emails
        </button>
      </td>
    </tr>
    <tr>
      <td>BLABLABLA</td>
    </tr>
    <tr>
      <td>BLABLABLA</td>
    </tr>
    <tr>
      <td>
        <input type="text" class="lstMails" value="myText2">
      </td>
    </tr>
  </tbody>
</table>

<div id="log"></div>

您的代码正在获取单击按钮的 tbody 祖先,然后尝试查找具有 lstMails 类的下一个兄弟 - 这不是您所追求的。

【讨论】:

  • 感谢您的回答。问题是 .lstMails 并不总是在下一个 tr 中,我已经编辑了我的 html 以显示一个示例。
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多