如何准备这个数组为$.ajax提交?
这里图像返回[“val1”,“val2”],但是在我使用$.param(图像)后,我得到undefined = undefined& undefined = undefined
这里图像返回[“val1”,“val2”],但是在我使用$.param(图像)后,我得到undefined = undefined& undefined = undefined
$('#rem_images').click(function(){ var images = new Array(); $('.images_set_image input:checked').each(function(i){ images[i] = $(this).val(); }); alert($.param(images)); return false;
解决方法
你没有以正确的格式传递数组到$.param.从
jQuery.param
docs:
If the object passed is in an Array,it must be an array of objects in the format returned by 07001.
数组应该是由名称/值对组成的对象数组.你看到undefined = undefined& undefined = undefined因为“val1”.name,“val1”.value,“val2”.name和“val2”.value都是未定义的.它应该看起来像这样:
[{name: 'name1',value: 'val1'},{name: 'name2',value: 'val2'}]
所以你可以这样构造数组(假设你的复选框有一个name属性):
$('#rem_images').click(function(){ var images = []; $('.images_set_image input:checked').each(function(){ var $this = $(this); images.push({name: $this.attr('name'),value: $this.val()}); }); alert($.param(images)); return false; });
$('#rem_images').click(function(){ var images = $('.images_set_image input:checked').map(function(){ var $this = $(this); return {name: $this.attr('name'),value: $this.val()}; }).get(); alert($.param(images)); return false; });