jquery implements the concrete implementation of form forms via Ajax methods _jquery

Source: Internet
Author: User
Today I just saw jquery Ajax's method of submitting data to the server, the original is:

Saves data to the server and displays information when it succeeds.
JQuery Code:
Copy Code code as follows:

$.ajax ({
Type: "POST",
URL: "some.php",
Data: "Name=john&location=boston",
Success:function (msg) {
Alert ("Data Saved:" + msg);
}
});

Then I thought about it, do I have to submit form form? But I can't write a var demo=$ ("#divname") for each fom input. val ().
Later, today I saw a method, that is. Map, to make the idea, you can learn from Yo;
The HTML code is as follows, and I'm going to submit all input data for form with ID Dlg_form
Copy Code code as follows:

<form id= "Dlg_form" method= "POST" >
<div class= "Fitem" >
<label> Room:</label>
<input name= "Roomname" style= "PADDING:2PX; width:135px; border:1px solid #A4BED4; "Required/>
</div>
<div class= "Fitem" >
<label> Building:</label>
<input name= "Roomname" class= "Easyui-combobox" style= "padding:2px"; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> Department:</label>
<input name= "Roomname" class= "Easyui-combobox" style= "padding:2px"; width:141px; "Required/>
</div>
<fieldset>
<legend>
<label>
<input type= "checkbox" id= "Ktkzq" name= "Ktkzq" value= "Ktkzq"/>
Air Conditioner Controller </label>
</legend>
<div class= "Fitem" >
<label> Port:</label>
<input name= "KT_DK" id= "KT_DK" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> Address:</label>
<input name= "Kt_dz" id= "Kt_dz" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> Working mode:</label>
<input name= "KT_GZFS" id= "KT_GZFS" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> whether to enable:</label>
<input name= "kt_sfqy" id= "kt_sfqy" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
</fieldset>
<fieldset>
<legend>
<label>
<input type= "checkbox" id= "Dgkzq" name= "Dgkzq" value= "Dgkzq"/>
Lighting Controller </label>
</legend>
<div class= "Fitem" >
<label> Port:</label>
<input name= "DG_DK" id= "DG_DK" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> Address:</label>
<input name= "Dg_dz" id= "Dg_dz" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> Working mode:</label>
<input name= "DG_GZFS" id= "DG_GZFS" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
<div class= "Fitem" >
<label> whether to enable:</label>
<input name= "dg_sfqy" id= "dg_sfqy" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
</fieldset>
<div class= "Fitem" >
<label style= "width:100px;" >
<input type= "checkbox" id= "Zongbiao" name= "Zongbiao" value= "Zongbiao"/>
The general table:</label> is installed
</div>
<div class= "Fitem" >
<label> General table Power node:</label>
<input name= "Zbdnjd" id= "ZBDNJD" disabled= "Disabled" class= "Easyui-combobox" style=; width:141px; "Required/>
</div>
</form>

is not a lot, if you want to write every input, is not to vomit blood?
Look at my method, first we take all the input name and value down,
The JS code is as follows:
Copy Code code as follows:

var str_data=$ ("#dlg_form input"). Map (function () {
Return ($ (a). attr ("name") + ' = ' +$ (this). Val ());
}). Get (). Join ("&");
alert (data);

PS: You alert, you will find that the structure inside this is divname=xxx&divname2=xxxx and so on,

And then looking back at Ajax submitted by:
Copy Code code as follows:

$.ajax ({
Type: "POST",
URL: "some.php",
Data: "Name=john&location=boston",
Success:function (msg) {
Alert ("Data Saved:" + msg);
}
});

Did you find out that if we put the above in the data, we can get it?

Complete code that should be modified after the
Copy Code code as follows:

$.ajax ({

var str_data=$ ("#dlg_form input"). Map (function () {
Return ($ (a). attr ("name") + ' = ' +$ (this). Val ());
}). Get (). Join ("&");
Type: "POST",
URL: "some.php",
Data:str_data,
Success:function (msg) {
Alert ("Data Saved:" + msg);
}
});

OK, it is so simple, if applicable, you can take it with yo ...

Oh.

If you have any questions, you are welcome to come forward.
Related Article

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.