jquery serialize (), Serializearray () has $.param method

Source: Internet
Author: User

Serialize

The Sequence table table content is a string that is used for Ajax requests . Can be for the entire form, or only for a certain part.

HTML Code:
<p id="results"><b>Results: </b> </p><form>  <select name="single">    <option>Single</option>    <option>Single2</option>  </select>  <select name="multiple" multiple="multiple">    <option selected="selected">Multiple</option>    <option>Multiple2</option>    <option selected="selected">Multiple3</option>  </select><br/>  <input type="checkbox" name="check" value="check1"/> check1  <input type="checkbox" name="check" value="check2" checked="checked"/> check2  <input type="radio" name="radio" value="radio1" checked="checked"/> radio1  <input type="radio" name="radio" value="radio2"/> radio2</form>
JQuery Code:
$("#results").append( "<tt>" + $("form").serialize() + "</tt>" );


$ (' #form '). Submit (Function (event) {
Event.preventdefault ();

$.ajax ({
URL: ",
Type: ' Post ',
data:$ ("form"). Serialize (),

}

Reference: http://api.jquery.com/serialize/

Serializearray ():

Encode a set of form elements as an array of names and values.

Definition and usage

The Serializearray () method creates an array of objects (names and values) by serializing the form values.

You can select one or more form elements (such as input and/or textarea), or the form element itself.

Grammar
$ (selector). Serializearray ()

The Serializearray () method serializes the form element (similar to the. Serialize () method) and returns the JSON data structure.

Note: This method returns a JSON object rather than a JSON string. You need to use a plug-in or a third-party library for string manipulation.

The returned JSON object consists of an array of objects, each of which contains one or two name-value pairs of the--name parameter and the value parameter if value is not empty. For example:

[   {name: ' FirstName ', Value: ' Hello '},   {name: ' LastName ', Value: ' World '},  {name: ' Alias '},//value is empty]

The. Serializearray () method uses the standard of the successful controls (active control) to detect which elements should be included. Note that the element cannot be disabled (the disabled element is not included ), and the element should have a name attribute. The value of the Submit button is also not serialized. The data for the file selection element is also not serialized.

This method can operate on objects that have selected individual form elements, such as <input>, <textarea>, and <select>. However, a more convenient way is to directly select the <form> tag itself for the serialization operation.

$ ("form"). Submit (function () {  Console.log ($ (this). Serializearray ());  return false;});

The above code produces the following data structure (assuming the browser supports Console.log):

[  {    name:a    value:1  },  {    name:b    value:2  },  {    name:c    value:3  },  {    name:d    value:4  },  {    name:e    value:5  }]

Get the form content and insert it into the Web page:

 <P><b>Results:</b> <spanID= "Results"></span></P><form>  <Selectname= "single">    <option>Single</option>    <option>Single2</option>  </Select>  <Selectname= "multiple"multiple= "multiple">    <optionselected= "Selected">Multiple</option>    <option>Multiple2</option>    <optionselected= "Selected">Multiple3</option>  </Select>  <BR>  <inputtype= "checkbox"name= "Check"value= "Check1"ID= "Ch1">  <label for= "Ch1">Check1</label>  <inputtype= "checkbox"name= "Check"value= "Check2"checked= "Checked"ID= "CH2">  <label for= "CH2">Check2</label>  <inputtype= "Radio"name= "Radio"value= "Radio1"checked= "Checked"ID= "R1">  <label for= "R1">Radio1</label>  <inputtype= "Radio"name= "Radio"value= "Radio2"ID= "R2">  <label for= "R2">Radio2</label></form> <Script>  functionshowvalues () {var Fields= $( ": Input"). Serializearray (); $( "#results"). empty (); Jquery.each (Fields,function(i, field) {$ ("#results"). Append (Field.value+ " " );  }); }   $( ": CheckBox,: Radio"). Click (showvalues); $( "Select"). Change (ShowValues); ShowValues ();</Script> </Body></HTML>

$(":input")                  选择所有的表单输入元素,包括input, textarea, select 和 button.


Reference:
Http://www.w3school.com.cn/jquery/ajax_serializearray.asp
http://api.jquery.com/serializeArray/

$.params()

Serialize a key/value object:

var params = {width:1900, height:1200};var str = jquery.param (params); $ ("#results"). Text (str);

Results:

width=1680&height=1050
Reference: http://www.w3school.com.cn/jquery/ajax_param.asp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.