2016-07-08 77 views
0

与天然形式的DOM元素我可以通过输入名称来访问它的领域:jquery等价于form.field_name?

<form id="form"> 
    <input type="text" name="input-name" /> 
</form> 

var form = document.getElementById("form"); 
form["input-name"] // or form.input_name if it wasn't an hyphen. 

会是怎样与jQuery一样吗?同样我的意思是相同的查找。 使用form.find("[name='input-name']")在性能方面不尽相同,表单本机方法仅仅是一个对象查找,jquery将使用querySelectorAll。

+1

'$( '#FORM_ID [NAME = “FIELD_NAME”]')'__OR__'$( '#FORM_ID')。找到( '[NAME = “FIELD_NAME”]')' – Rayon

回答

0

$('#form [name="input-name"]')

0

你可以做到这一点使用Attribute Equals Selector [name=”value”]: -

$('#form [name="input-name"]') 
// Find the elements with name attribute selector within a form with id `form` 

或者使用.find()

$('#form').find('[name="input-name"]') 
// Search through the descendants of `#form` in the DOM tree 

或者,如果你可以把一些id来输入文字,就可以只是做

$('#inputID') 
// As id are supposed to be unique in DOM no need to Search through 
// the descendants of `#form`, just call the element by ID 
0

可以通过使用下面的选择器获得对象:

$( “输入[类型= '文本'] [名= '输入名']”);

+0

但那是在雪上加霜性能方面,使用本地DOM表单元素,它仅仅是一个对象索引。我无法访问表单对象元素,就好像它是一个本地DOM元素一样? – user3599803