When submitting a form through jquery Ajax, the form content is usually obtained in the following way.
varform = ' Add-account-form '; //form ID $ (' # ' + form). Submit (function() {varAccount = { ' user_id ': ', ' Partner_name ': ', ' Site_url ': ',' Product_url ': '}; for(varIinchAccount ) { if(Account.hasownproperty (i)) { The//hasownproperty function determines whether a member is contained in an object. if($ (' # ' + form + ' input[name= ' + i + '] '). Length > 0) account[i] = $ (' # ' + form + ' input[name= ' + i + '] '). Val (); ///This can only be used in the form of account[i], cannot be used account.i if($ (' # ' + form + ' select[name= ' + i + '] '). Length > 0) account[i] = $ (' # ' + form + ' select[name= ' + i + '] '). Val (); }} $.post ('/pc/account ', {' data ': Account, ' op ': ' Add '},function(data) {}); });
1.serialize ()
You can pass the Serialize method of jquery, because $.get, $.post, $.ajax data part can be mapped form {"name": "Lee", "Age": "18"}, can also be passed as a string name=lee&age=18.
var form = ' Add-account-form '; $ (' # ' + form). Submit (function() { var account = $ (this). Serialize ( ); //Gets all the name values of the form as a string of value Key,val, with a & connection. $.post (function(data) { }); });
Attention:
Use the string method to note the character encoding (Chinese), with the Serialize () method will be automatically encoded.
The serialize () method is used for jquery objects, so not only can the form be used, but other selectors can also be used, such as $ (": CheckBox,: Radio"). Serialize ();
<input type= "checkbox" name= "Check" value = "Chinese" /> Chinese < input type = "checkbox" name = "Check" value = "American" /> American
/ * If two multiple boxes are selected, the Serialize method does not merge values, but produces several values with the same name * /
$ ("form"). Serialize (); check=%e4%b8%ad%e5%9b%bd%e4%ba%ba&check=%e7%be%8e%e5%9b%bd%e4%ba%ba//Generate check=***&check= form, and Chinese is coded
2.serializeArray ()
Instead of returning a string, the method returns the JSON format after serializing the DOM element.
var f = $ (": CheckBox,: Radio"). Serializearray (); // get the value of the Console.log box and the Radio box (f); // viewing in the console
$.each function (I, field) { //f is an object that can be traversed by the $.each method
$ ("$result"). Append (Field.value + ",");
})
3.$.param ()
He is the core of the Serialize () method, which is used to serialize arrays and objects in the form of Key/value.
var obj = {a:1,b:2,c:3}var s = $.param (obj); // a=1&b=2&c=3
The role of sharp jquery-6--serialization functions serialize () and Serializearray () in form submissions