Home > Web Front-end > JS Tutorial > jQuery uses serialize() to submit form data based on ajax()_jquery

jQuery uses serialize() to submit form data based on ajax()_jquery

WBOY
Release: 2016-05-16 15:26:52
Original
1600 people have browsed it

The example in this article describes how jQuery uses serialize() to submit form data based on ajax(). Share it with everyone for your reference, the details are as follows:

jQuery’s serialize() method creates a URL-encoded text string by serializing form values. We can select one or more form elements, or directly select form to serialize them, such as:

<form action="">
First name: <input type="text" name="FirstName" value="Bill" /><br />
Last name: <input type="text" name="LastName" value="Gates" /><br />
</form>
<script>
$(document).ready(function(){
 console.log($("form").serialize());
 // FirstName=Bill&LastName=Gates
});
</script>

Copy after login

In this way, we can pass the serialized value to ajax() as a parameter of the url, and easily use ajax() to submit the form, instead of getting the values ​​in the form one by one and passing them to ajax(). Examples are as follows:

$.ajax({
 type: 'post',
 url: 'your url',
 data: $("form").serialize(),
 success: function(data) {
  // your code
 }
});

Copy after login

The same goes for using $.post(), $.get() and $.getJSON():

$.post('your url', $("form").serialize(), function(data) {
  // your code
 }
});
$.get('your url', $("form").serialize(), function(data) {
  // your code
 }
});
$.getJSON('your url', $("form").serialize(), function(data) {
  // your code
 }
});

Copy after login

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template