使用jQuery在同一Div中选择与另一个元素相同的元素类型

使用jQuery在同一Div中选择与另一个元素相同的元素类型,jquery,input,click,Jquery,Input,Click,我是jQuery新手,有一个我认为是基本问题的问题。我在谷歌和jQuery网站上搜索过关于“父母”和“孩子”的信息,但我很难用语言表达我想要实现的目标 假设我有以下标记: <div> <input type="text" value="John Doe" /> <a href="#" class="clearButton">clear</a> </div> <div> <input type=

我是jQuery新手,有一个我认为是基本问题的问题。我在谷歌和jQuery网站上搜索过关于“父母”和“孩子”的信息,但我很难用语言表达我想要实现的目标

假设我有以下标记:

<div>
    <input type="text" value="John Doe" /> 
    <a href="#" class="clearButton">clear</a>
</div>
<div>
    <input type="text" value="Jane Doe" /> 
    <a href="#" class="clearButton">clear</a>
</div>
<div>
    <input type="text" value="Joe Smith" /> 
    <a href="#" class="clearButton">clear</a>
</div>

我主要想编写一点jQuery,使每个“clearButton”链接在单击时清除其同级输入的值。考虑到我的标记,这可能吗?或者每个输入或每个div都需要唯一的标识符?是否可以接收单击,然后使用“this”命令选择正确的同级输入?我使用同级语言编写了自己的一段代码,但它一次清除了所有输入

非常感谢您提供相关信息的任何提示或链接!
谢谢

您可能只需要在代码中附加
.first()
。它应该是这样的:

$('.clearButton').click(function() {
    $(this).siblings().first().val('');
});
以及


如果链接始终位于输入字段旁边:

$('.clearButton').click(function() {
    $(this)          // the clearButton
          .prev()    // get the input field
          .val('');  // clear its value

    return false;    // disable default link action (otherwise # adds to url)
});

谢谢你,马丁!我还要感谢所有提供解决方案的人——我真的很感谢他们的帮助。
$('.clearButton').click(function() {
    $(this)          // the clearButton
          .prev()    // get the input field
          .val('');  // clear its value

    return false;    // disable default link action (otherwise # adds to url)
});
$('.clearButton').click(function () {
   $(this).sibling('input').val('');   
});