[英]jQuery .ajax() POST Only checkboxes :checked
I am getting an error in my .ajax() function when attempting to pass in the checkboxes 尝试传递复选框时,我的.ajax()函数出现错误
Here is the code: 这是代码:
if(typeof($post) !== 'undefined'){
var $fname = $($post).attr('name').toString();
var data = {$fname : []};
alert($post);
alert($fname);
$($post + ":checked").each(function() {
data[$fname].push($(this).val());
});
}else{
var data = null;
}
The error I am getting in firebug is: data[$fname].push($(this).val()); is undefined
我在萤火虫中遇到的错误是:
data[$fname].push($(this).val()); is undefined
data[$fname].push($(this).val()); is undefined
$post
is just a class name passed into the function.. in this case it's .del-checked
$post
只是传递给函数的类名。在这种情况下,它.del-checked
The alerts sucessfully alert me the class name, and the checkbox name... in this case it's del[]
警报成功地向我警报了类名和复选框名...在这种情况下,它是
del[]
How can I get this to work in order to pass it to the data
option of $.ajax
? 我如何使它工作以便将其传递给
$.ajax
的data
选项?
Because you can not use a variable as a key when creating a new object 因为在创建新对象时不能将变量用作键
var data = {$fname : []};
is the same thing as doing 和做一样
var data = {"$fname" : []};
You need to create the object and add the key with brackets 您需要创建对象并在方括号中添加密钥
var data = {};
data[$fname] = [];
You can't use variables as keys unless you use bracket notation 除非使用方括号符号, 否则不能将变量用作键
if (typeof($post) !== 'undefined'){
var $fname = $($post).attr('name');
var data = {};
data[$fname] = [];
$($post).filter(":checked").each(function() {
data[$fname].push( this.value );
});
}else{
var data = null;
}
关于什么:
var data = $($fname).serialize();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.