admin管理员组文章数量:1314315
In the following code, I'm trying to send a key-value pair and I always get the error:
" missing: after property id "
$(".general").change(function () {
fields = { $(this).attr('id') : "1" };
$.ajax({
type: "POST",
url: "ajax/update_general.php",
data: { fields: fields },
dataType: "json",
});
})
I've figured that what causes the problem is:
$(this).attr('id')
But I have no clue why. I've tried to first assign $(this).attr('id') to a variable, and put the variable in the ajax call, but that didn't help.
How can I fix that?
Thank you!
In the following code, I'm trying to send a key-value pair and I always get the error:
" missing: after property id "
$(".general").change(function () {
fields = { $(this).attr('id') : "1" };
$.ajax({
type: "POST",
url: "ajax/update_general.php",
data: { fields: fields },
dataType: "json",
});
})
I've figured that what causes the problem is:
$(this).attr('id')
But I have no clue why. I've tried to first assign $(this).attr('id') to a variable, and put the variable in the ajax call, but that didn't help.
How can I fix that?
Thank you!
3 Answers
Reset to default 8It's a syntax error. You can't use the return value of a function call as a property name.
You can, however, use that return value in bracket notation after initializing the object:
fields = {};
fields[$(this).attr('id')] = '1';
When declaring object with {} syntax, ONLY strings (like {'foo':1}) or bare string is allowed ({foo:1})
You should write something like this:
var fields = {};
fields[$(this).attr('id')] = 1;
Change this line:
fields = { $(this).attr('id') : "1" };
to this:
fields = $(this).attr('id') || "1";
That's if you intended to have something like a default value.
If you want an object, use this:
fields[$(this).attr('id')] = "1";
本文标签: javascriptError 39 missingafter property id39 when using Jquery ajax functionStack Overflow
版权声明:本文标题:javascript - Error ' missing : after property id' when using Jquery ajax function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741921440a2405026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论