2014-10-20 62 views
0

请参阅此代码形式的订单号:获取所有输入,选择和按钮,并使用jQuery

<form id=form1> 
    <input name="TextBox1" type="text" value="1111111111" id="TextBox1" /> 
    <input name="TextBox2" type="text" value="222222222" id="TextBox2" /> 
    <select name="DropDownList1" id="DropDownList1"> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
    </select> 
    <input id="CheckBox1" type="checkbox" name="CheckBox1" /> 
    <label for="CheckBox1">nima</label> 
    <input id="Button1" type="button" value="button" /> 
</form> 

我想使用jQuery与秩序number.For例如获得在形式上所有控制元件:

ElementID  order number 
-------------------------------- 
TextBox1    1 
TextBox2    2 
DropDownList1   3 
CheckBox1    4 
Button1    5 

我该怎么做? 感谢

+2

BTW:这是_顺序_号码,而不是_hierarchy_号 – hindmost 2014-10-20 10:20:55

回答

1

内得到其为了这不是层号,它是指数。

$.each($('#form1').find('input, select'), function() 
{ 
    alert('Index: ' + $(this).index()); 
}); 

或者,如果您需要连续数

$.each($('#form1').find('input, select'), function(counter) 
{ 
    alert('Index: ' + counter); 
}); 
2

您可以使用:input.each()的订单号:

$('#form1 :input').each(function(index) 
{ 
    console.log(this.id + " " + (index + 1)); 
}); 

Fiddle

1

你可以得到所有的孩子形式的元素做$('#form1').children()将返回表格的唯一直接子(即没有来自<select>内部的<option>标签)。

然后,您可以调用的.index()每个元素的形式http://api.jquery.com/index/

相关问题