简体   繁体   中英

Javascript selecting form elements using name attribute

Is it possible to select a form element using it's name attribute?

For example if I had something like this:

<input type="text" name="my_element" />

How would I go about setting a javascript variable to the value of this input?

var name_val = $(input[name='my_element']).val();

?

Your almost there

var name_val = $('input[name=my_element]').val();
var name_val = $('input[name="my_element"]').val();

The following should work:

var name_val = $('input[name="my_element"]').val();

It's even more simple: $("#form_id").elements["my_element"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM