在下面的代码中,我试图发送一个键值对,但我总是收到错误:
“缺少:属性 ID 之后”
$(".general").change(function () {
fields = { $(this).attr('id') : "1" };
$.ajax({
type: "POST",
url: "ajax/update_general.php",
data: { fields: fields },
dataType: "json",
});
})
我认为导致问题的原因是:
$(this).attr('id')
但我不知道为什么。我试图首先将 $(this).attr('id') 分配给一个变量,然后将该变量放入 ajax 调用中,但这没有帮助。
我该如何解决?
谢谢!
最佳答案
这是语法错误。您不能将函数调用的返回值用作属性名称。
但是,您可以在初始化对象后使用方括号表示法中的返回值:
fields = {};
fields[$(this).attr('id')] = '1';
关于javascript - 使用jquery ajax函数报错 ' missing : after property id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5613185/