jQuery:找到最接近按钮的输入字段 [英] jQuery: find input field closest to button
问题描述
我有一个带有多个输入字段的大型HTML表单(每行一个)。
其中我有几个字段,其中有一个特定的类,它们出现在按钮的左边。
这些行中没有其他按钮或字段,因此它们只包含下面的字段和按钮。
如何获取此字段的ID点击右边的按钮,即最接近它?
有问题的所有字段如下所示:
< input type =textclass =dateFielddata-date-format =yyyy-mm-ddid =field1name =field1/>
有问题的所有按钮都是这样的:
< button type =button>选择日期< / button>
非常感谢Tim的帮助。
<按钮>的左侧$ c> ('click',function()); $ c $> $('button')。 {
alert($(this).prev('input')。attr('id'));
});
如果<输入> ;
位于< button>
之后,那么您可以像这样找到它:
<$ p ('click',function(){
alert($(this).next('input')。attr('id'))$ p> $('button')。 );
});
I have a large HTML form with multiple input fields (one in each row). Among these I have several fields with a specific class that appear left from a button. There are no other buttons or fields in these rows so they only contain the field and button as below.
How can I get the ID of such a field when clicking on the button right from it, i.e. closest to it ?
All fields in question look like this:
<input type="text" class="dateField" data-date-format="yyyy-mm-dd" id="field1" name="field1" />
All buttons in question look like this:
<button type="button">Select Date</button>
Thanks for any help with this, Tim.
Because <input>
is to the left of <button>
you can find it like this:
$('button').on('click', function(){
alert($(this).prev('input').attr('id'));
});
If <input>
was after <button>
then you can find it like this:
$('button').on('click', function(){
alert($(this).next('input').attr('id'));
});
这篇关于jQuery:找到最接近按钮的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!