【问题标题】:jQuery get the next sibling hidden value on clickjQuery在点击时获取下一个兄弟隐藏值
【发布时间】:2011-09-30 09:07:05
【问题描述】:

我正在尝试获取链接点击事件的隐藏字段值。以下是这些元素所在的位置:

<p>
    <a class="showCommentAttachment cboxElement" href="#">Attachments</a>
    <input type="hidden" value="13" id="ctl00_ContentPlaceHolder1_lvComment_ctrl3_hfCommentId" name="ctl00$ContentPlaceHolder1$lvComment$ctrl3$hfCommentId">
</p>

这里是完整的标记:

<div id="divComment" class="comment-text">
    <div class="comment-author">
         David Chart
    </div>
    <span class="comment-date-time">Sep 29, 2011 08:12:42 PM</span>
    <p>
         Some comment text goes here. Some comment text goes here.

    </p>
    <p>
        <a class="showCommentAttachment cboxElement" href="#">Attachments</a>
        <input type="hidden" value="13" id="ctl00_ContentPlaceHolder1_lvComment_ctrl3_hfCommentId" name="ctl00$ContentPlaceHolder1$lvComment$ctrl3$hfCommentId">
    </p>
</div>

这里是 jQuery:

$("#divComment a.showCommentAttachment").click(function() {

    var nextSibling = $(this).next().find('input:hidden').val();

    $("#showCommentId").html(nextSibling + "<-- Here's the comment ID ");

 });

我从 nextSibling 得到的结果是未定义的。我试过这样的nexAll

var nextSibling = $(this).nextAll().find('input:hidden').val();

仍然未定义。

谢谢。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您应该简单地使用$(this).next(),因为下一个元素肯定是隐藏的输入字段。同样使用$(this).next().find(),您正在查询隐藏输入元素的子元素。这是来自jQuery API documentation

    .find()

    获取当前匹配元素集中每个元素的后代,由选择器、jQuery 对象或元素过滤。

    所以你需要$(this).next().val()

    【讨论】:

      【解决方案2】:

      .next() 为您提供下一个兄弟,即输入字段本身。然后调用.find() 搜索该字段的后代(没有任何后代)。

      你只想要$(this).next().val()

      【讨论】:

        【解决方案3】:

        请尝试:

        $(this).next().val();
        

        $("#divComment a.showCommentAttachment").next().val();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-05
          • 2010-10-26
          • 2013-01-31
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多