I am creating the UI of a web app, and haven't done any back-end work yet.
For testing purposes, I want my forms to be able to work, so I want to take the input they have received and store it in javascript variables so I can access them, using jQuery.
Everything I have looked 开发者_如何学编程at, tells me how to pass js variables into the form, not the other way around.
Any ideas?
Thanks.
var variable = $('#myInput').val()
. Or if your form is submitting using GET
, you may want to look at the jQuery URL Parsing plugin to get access to the variables on the new page.
Edit:
Sample form (should be a simple text box):
<html>
<body>
<form action="#">
<input type="text" id="myInput" name="myInput"/>
</form>
</body>
</html>
With that form you can use the above sample code. Incidentally, you can use the .val()
method on any jQuery selector that returns form elements, e.g. $('input:text').val()
will get you the value of all text input fields (as an array if there are multiple values).
I ran across this pretty awesome question on how to serialize a form to JSON with jQuery which would be a good start.
You can also take a look at the jQuery Form plugin which provides the formSerialize method that
Serializes the form into a query string. This method will return a string in the format: name1=value1&name2=value2
as well as a fieldSerialize method which
Serializes field elements into a query string. This is handy when you need to serialize only part of a form. This method will return a string in the format: name1=value1&name2=value2
精彩评论