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
WBOYOriginal
2016-05-16 15:26:521602browse

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>

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
 }
});

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
 }
});

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

Statement:
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