简体   繁体   中英

“Correct” Way to Access Form Element Values with JQuery

As anybody who has written javascript/jquery knows, there's a lot of different ways to do the same thing. I'm currently trying to send some data to the server on form submit and as an AJAX request, and I was wondering what the "correct" way of doing this was.

I was tentatively going to just access all the form elements by ID and then pull their values, but I know you can also iterate through them. What is typically done in this situation?

If you have inputs and selects in your form, you can just serialize the data.

$( "input, select" ).serialize();

you can iterate over the fields and then populate the object. but what happens if you have many fields in your form. It's leads to lots of extra code when you have a method that abstracts all this.

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