找到相同元素类型的下一个兄弟

时间:2015-06-02 20:45:09

标签: jquery

这是一个表格:

<form>
  <input type="text" name="name" id="name">
  <label>Phone number *</label>
  <input type="number" name="phone" id="phone">
  <label>Email address *</label>
  <input type="email" name="email" id="email">
  <h3>Event</h3>
  <label>Event type *</label>
  <input type="text" name="type" id="type">
  <input type="text" name="address" id="address">
  <input type="date" name="date" id="date">
</form>

我将变量初始化为第一个input元素:

var cur_input;

我想遍历表单元素,在每次迭代中将下一个输入元素分配给cur_input变量。

我试过了:

cur_input = cur_input.next(); //failed because there are other elements in between
cur_input = cur_input.next('input'); //failed for some reason.

1 个答案:

答案 0 :(得分:1)

您可以使用

找到相同元素类型的下一个兄弟
var inputs = $('form input');        // Get all input under form
var index = inputs.index(cur_input); // Get current index
var next_input = inputs.eq(index+1); // Get next input
相关问题