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:
$.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,
是不是很多,如果要你每个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
$.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.