找到“下一个”的最佳方式在jQuery中形成输入元素? [英] Best way to find "next" form input element in jQuery?
问题描述
使用jQuery,从任意元素开始,在页面上查找下一个表单元素的最佳方法是什么?当我说表单元素时,我的意思是< input>
,< select>
,< ;按钮>
或< textarea>
。
Using jQuery, what's the best way to find the next form element on the page, starting from an arbitrary element? When I say form element I mean <input>
, <select>
, <button>
or <textarea>
.
在以下示例中, id为this的元素是任意起始点,id为next的元素是我想要查找的元素。所有示例都应该使用相同的答案。
In the following examples, the element with the id "this" is the arbitrary starting point, and the element with the id "next" is the one I want to find. The same answer should work for all examples.
示例1:
<ul>
<li><input type="text" /></li>
<li><input id="this" type="text" /></li>
</ul>
<ul>
<li><input id="next" type="text" /></li>
</ul>
<button></button>
示例2:
<ul>
<li><input id="this" type="text" /></li>
</ul>
<button id="next"></button>
示例3:
<input id="this" type="text" />
<input id="next" type="text" />
示例4:
<div>
<input id="this" type="text" />
<input type="hidden" />
<div>
<table>
<tr><td></td><td><input id="next" type="text" /></td></tr>
</table>
</div>
<button></button>
</div>
编辑:到目前为止提供的两个答案都需要为页面上的所有输入元素写一个序列号。正如我在其中一个人的评论中提到的,这就是我已经在做的事情,我更希望有一个只读解决方案,因为这将发生在一个插件中。
The two answers provided so far both require writing a sequence number to all input elements on the page. As I mentioned in the comments of one of them, this is kind of what I'm already doing and I would much prefer have a read-only solution since this will be happening inside a plugin.
推荐答案
kudos,
如何使用.index?
What about using .index?
例如 $(':input:eq('+($(':input')。index(this)+ 1)+')');
这篇关于找到“下一个”的最佳方式在jQuery中形成输入元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!