本文介绍了jQuery 在单击时获取下一个兄弟隐藏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这里是完整的标记:

<div class="comment-author">大卫图

<span class="comment-date-time">Sep 29, 2011 08:12:42 PM</span><p>一些评论文本放在这里.一些评论文本放在这里.</p><p><a class="showCommentAttachment cboxElement" href="#">附件</a><input type="hidden" value="13" id="ctl00_ContentPlaceHolder1_lvComment_ctrl3_hfCommentId" name="ctl00$ContentPlaceHolder1$lvComment$ctrl3$hfCommentId"></p>

这是jQuery:

$("#divComment a.showCommentAttachment").click(function() {var nextSibling = $(this).next().find('input:hidden').val();$("#showCommentId").html(nextSibling + "<-- 这里是评论 ID ");});

我从 nextSibling 得到的东西是不确定的.我像这样尝试过 nexAll

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

仍未定义.

谢谢.

解决方案

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

.find()

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

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

I'm trying to get the hidden field value on click event of a link. Here's where those elements are:

<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>

Here the complete mark-up:

<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>

Here's the jQuery:

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

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

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

 });

what I get back from nextSibling is undefined. I tried with nexAll like this

still got undefined.

Thank you.

解决方案

You should use simply $(this).next() since the next element is surely the hidden input field. Also with $(this).next().find() you are querying the children elements of the hidden input element. This is from jQuery API documentation:

So you need $(this).next().val().

这篇关于jQuery 在单击时获取下一个兄弟隐藏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:24