There are many ways to submit Form forms. This article describes how Jquery submits Form forms through Ajax. Today, we can see Jquery's ajax method to submit 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:
The Code is 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. Is there a way to submit the form? However, I cannot write var demo =$ ("# pname"). 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,
The Code is as follows:
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:
The 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 pname = xxx & pname2 = xxxx, etc,
Then let's look back at what ajax submitted:
The Code is as follows:
$. 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
The Code is as follows:
$. 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.