jquery查找另一个元素旁边的元素

jquery查找另一个元素旁边的元素,jquery,Jquery,您好,我有以下html: <p> <input type="text" name="field1"/> <input type="hidden" name="fieldh1"/> <button type="button" class="sendInfo">Send</button> </p> <p> <input type="text" name="field2

您好,我有以下html:

<p>  
    <input type="text" name="field1"/> <input type="hidden" name="fieldh1"/>  
    <button type="button" class="sendInfo">Send</button>  
</p>  
<p>  
    <input type="text" name="field2" /> <input type="hidden" name="fieldh2"/>  
    <button type="button" class="sendInfo">Send</button>  
</p>  
我计划将用户在textbox中键入的内容设置为隐藏字段,并将从ajax调用接收到的值设置为普通textbox。但问题是,我甚至无法获得与用户单击的按钮在同一行的文本框的值。 有人能帮我吗? 非常感谢。

试试:

$(this).siblings('input:text').val();
或者将
next
更改为
find

$(this).parent().find('[type=text]').val();

as
next
只搜索紧跟其后的兄弟姐妹。

我可以使用函数。

您可以使用id选择您的文本框吗

$(function() { 
   $('button.sendInfo').live('click', function() {
      //what about this:
      var id = $('#textBoxID').val();
      alert(id); 
       //or this
      var id2 = $('+ input', this).val(); 
      alert(id2);
    }); 
});
您的问题是“next()”将指向父级的下一个同级--父级是
标记,因此同级(如果存在)是以下
标记


您需要
$(this).parent().children('[type=text]').val()
$(this.parent().find('[type=text]')

否,因为我有许多文本框,不想单独为它们设置处理程序。我需要在按下按钮之前识别文本框!这很好:$(This.parent().find('[type=text]').val();谢谢你的帮助
$(function() { 
   $('button.sendInfo').live('click', function() {
      //what about this:
      var id = $('#textBoxID').val();
      alert(id); 
       //or this
      var id2 = $('+ input', this).val(); 
      alert(id2);
    }); 
});