Home  >  Article  >  Web Front-end  >  Specific implementation of Jquery submitting Form form through Ajax_jquery

Specific implementation of Jquery submitting Form form through Ajax_jquery

WBOY
WBOYOriginal
2016-05-16 17:16:561153browse

Today I happened to see Jquery's ajax method of submitting data to the server. The original text is:

Save the data to the server and display information when successful.
jQuery code:

Copy 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 any way I can submit the form? But I can’t input every fom Write var demo=$("#divname").val(); every time.
Later, today I saw a method, which is .map, and I made some ideas, which I can learn from;
The html code is as follows. Next I want to submit all the input data with the Form ID of dlg_form,
Copy the code The code is as follows:

































































是不是很多,如果要你每个input都写的话,是不是要吐血?
看看我的方法,首先我们把所有的input的name和value都取下来,
js代码如下:
复制代码 代码如下:

var str_data=$("#dlg_form input").map(function(){
return ($(this).attr("name") '=' $(this).val());
}).get().join("&") ;
alert(data);

ps:你alert一下,你会发现,这里面的架构就是divname=xxx&divname2=xxxx等等,

然后在回头看看ajax提交的:
复制代码 代码如下:

$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function (msg){
alert( "Data Saved: " msg );
}
});

Have you found anything? As long as we put what we obtained above, Just go into the data?

The complete code, after modification, should be
Copy the code 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, it’s that simple, if applicable, you can use it...

Hehe.

If you have any questions, you are welcome to ask them.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn