Review: Processing forms with jQuery
To process a form with jQuery, you should add an event listener to the form element for the 'submit' event:
$("form").on("submit", function() {
// process form
});
If you are processing the form entirely in jQuery, then you should call
preventDefault() to prevent the page reloading: $("form").on("submit", function(event) {
event.preventDefault();
// process form
});
When you want to know what a user filled out for an input in a form, you typically use
val(): var answer = $("#answer").val();
Inside the callback function, you can reference the form element using the
this keyword. A common pattern is to call find() on the form element, to find only inputs inside the form element: $("form").on("submit", function() {
// store the value of the input with name='age'
var age = $(this).find('[name=age]').val();
});
Nhận xét
Đăng nhận xét