2016-07-08 105 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