Jquery uses Ajax to submit Form

Source: Internet
Author: User

Today, we can see how Jquery ajax submits data to the server. The original Article is:


Save the data to the server. If the data is successfully saved, the information is displayed.

JQuery code:
$. Ajax ({
Type: "POST ",
Url: "some. php ",
Data: "name = John & location = Boston ",
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
}). I thought about it later. Is there any way to submit the form? However, I cannot write var demo =$ ("# divname"). val (); for every fom input.
Later, today I saw a method called. map, which I thought about and could be used for reference;
The html code is as follows. Next I want to submit all input data whose id is dlg_form,
<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 = "padding: 2px; width: 141px;" required/>
</Div>
<Div class = "fitem">
<Label> address: </label>
<Input name = "kt_dz" id = "kt_dz" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; width: 141px;" required/>
</Div>
<Div class = "fitem">
<Label> working method: </label>
<Input name = "kt_gzfs" id = "kt_gzfs" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; width: 141px;" required/>
</Div>
<Div class = "fitem">
<Label> enable or not: </label>
<Input name = "kt_sfqy" id = "kt_sfqy" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; 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 = "padding: 2px; width: 141px;" required/>
</Div>
<Div class = "fitem">
<Label> address: </label>
<Input name = "dg_dz" id = "dg_dz" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; width: 141px;" required/>
</Div>
<Div class = "fitem">
<Label> working method: </label>
<Input name = "dg_gzfs" id = "dg_gzfs" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; width: 141px;" required/>
</Div>
<Div class = "fitem">
<Label> enable or not: </label>
<Input name = "dg_sfqy" id = "dg_sfqy" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; width: 141px;" required/>
</Div>
</Fieldset>
<Div class = "fitem">
<Label style = "width: 100px;">
<Input type = "checkbox" id = "zongbiao" name = "zongbiao" value = "zongbiao"/>
Summary table installed: </label>
</Div>
<Div class = "fitem">
<Label> power nodes in the total table: </label>
<Input name = "zbdnjd" id = "zbdnjd" disabled = "disabled" class = "easyui-combobox" style = "padding: 2px; width: 141px;" required/>
</Div>
</Form>

Isn't it a lot? If you want to write every input, do you want to vomit blood?
Let's look at my method. First, we take down all the names and values of input,
The js Code is as follows:
Var str_data = $ ("# dlg_form input"). map (function (){
Return ($ (this). attr ("name") + '=' + $ (this). val ());
}). Get (). join ("&");

Alert (data );

Ps: You alert, you will find that the architecture here is divname = xxx & divname2 = xxxx, etc,

Then let's look back at what ajax submitted:


$. Ajax ({
Type: "POST ",
Url: "some. php ",
Data: "name = John & location = Boston ",
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});


Have you found that we only need to put the obtained data in data?

 


The complete code should be


$. Ajax ({var str_data = $ ("# dlg_form input"). map (function (){
Return ($ (this). attr ("name") + '=' + $ (this). val ());
}). Get (). join ("&");
Type: "POST ",
Url: "some. php ",
Data: str_data,
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
}); OK, it's that simple. If applicable, you can use it...

Haha.

If you have any questions, please submit them.

 


 

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.