javascript 单击按钮时,使用 JQuery 选择一个兄弟元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8623108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:01:27  来源:igfitidea点击:

On button click, select a sibling element with JQuery

javascriptjquery

提问by user619656

I have this code:

我有这个代码:

<div class="item">
  <div class="hidden">44</div>
  <input type="submit" id="btnAddCommentForAnswer" value="Add Comment" />
  <script type="text/javascript">
    $(document).ready(function () {
        $('#btnAddCommentForAnswer').click(function () {
            alert(XXX);
        });

    });
  </script>    
</div>

<div class="item">
  <div class="hidden">12</div>
  <input type="submit" id="btnAddCommentForAnswer" value="Add Comment" />
  <script type="text/javascript">
    $(document).ready(function () {
        $('#btnAddCommentForAnswer').click(function () {
            alert(XXX)
        });

    });
  </script>  
</div>

what code should I put at XXX to get the content of the div with class=hidden when i press the button at the same div with class=item?
If you click the first button you should get 44, and for clicking the second button you get 12.

当我按下带有 class=item 的同一个 div 上的按钮时,我应该在 XXX 上放什么代码来获取带有 class=hidden 的 div 的内容?
如果你点击第一个按钮,你应该得到 44,点击第二个按钮你得到 12。

回答by drfranks3

id'sare meant to be unique; you may wish to consider changing the idto a class. With that in place, you could use a script like the following to achieve what you want:

id's是独一无二的;您不妨考虑将 更改idclass。有了它,您可以使用如下脚本来实现您想要的:

$(document).ready(function() {
    $('.btnAddCommentForAnswer').click(function() {
        alert($(this).siblings('.hidden').text());
    });
});

Here's a JSFiddle example: JSFiddle

这是一个 JSFiddle 示例:JSFiddle

Also, you don't needto have two script tags in your script! This one script wrapped in <script>tags is all that is necessary :)

此外,您的脚本中不需要有两个脚本标签!这个用<script>标签包裹的脚本就是必需的:)

回答by user424134

Haven't tried but this should work:

没试过,但这应该有效:

$(this).closest('div').text()

回答by Virendra

You need not repeat the javascript code multiple times. Also, you cannot use id in jquery as the buttons have same id. Here is the jsfiddle http://jsfiddle.net/t8XJZ/which might solve your problem.

您无需多次重复 javascript 代码。此外,您不能在 jquery 中使用 id,因为按钮具有相同的 id。这是 jsfiddle http://jsfiddle.net/t8XJZ/可能会解决您的问题。

回答by Salt Hareket

maybe this is more useful for you...

也许这对你更有用......

jquery:

查询:

$(document).ready(function () {
     $('.item').each(function(){
         $(this).find("#btnAddCommentForAnswer").click(function () {
            alert($(this).prev(".hidden").text())
        });
    });
 });

html:

html:

<div class="item">
  <div class="hidden">44</div>
  <input type="submit" id="btnAddCommentForAnswer" value="Add Comment" />  
</div>

<div class="item">
  <div class="hidden">12</div>
  <input type="submit" id="btnAddCommentForAnswer" value="Add Comment" />
</div>