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 );   }});
Then I thought about it. Is there a way to submit the form? However, I cannot write var demo =$ ("# divname") for every fom input "). val. later, today I saw a method, that is. map, let's take a look at the idea. You can use it for reference. The HTML code is as follows. Below 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> is there a lot of data? If you want to write every input, is it necessary to vomit blood? Let's take a 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, that's 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.