I'm stumped on this. I'm trying to send an AJAX call by iterating over all the inputs and collecting their val()
which is a string.
So that my params would hopefully look like this:
"action"=>"create",
"type"=>"zip",
"value"=> ["12", "13", "14", "14", "15", "16"],
"controller"=>"admin/distributions",
"email_id"=>"3"}
This is what I have, but its giving me an [Object object]
as a value :
$(".all_of_morris").live("click", function(){
id = window.location.href.split("/")[5]
$.ajax({
type: "POST",
url: "/admin/emails/" + id + "/distributions",
dataType: "script",
data: { $.each($(".morris input"), function()开发者_如何转开发{
value: $(this).val();
}),
type: "zip" }
});
});
If you need an array for the value property, you can use the jquery map function:
$(".all_of_morris").live("click", function(){
var data = {};
data.value = $(".morris input").map(function(){
return $(this).val();
}).get();
data.type = 'zip';
//..etc
id = window.location.href.split("/")[5]
$.ajax({
type: "POST",
url: "/admin/emails/" + id + "/distributions",
dataType: "script",
data: data
});
});
How about using serialize()
?
$('.morris input').serialize();
And you don't want to an .each
within the json object. If pre-processing is required, build the payload in a variable first using whatever means you deem necessary, then pass that variable to the .ajax
call.
e.g.
var value = /*get value array */; ...
var dataToPass = {
'action' : 'create',
'value': value,
'type' : 'zip',
...
};
$.ajax({
...
data: dataToPass,
...
});
It returns an object because the each
of JQuery merely executes its contents, not return it. The data key also accepts strings as value so you could iterate through the contents, generate something like "action=create&type=zip&..."
and then give that array as value for the data key.
data: { value: $('.morris input').map( function() {
return $(this).val();
}).get(),
type: 'zip'
},
精彩评论