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!

Share Improve this question edited Feb 23, 2017 at 8:16 mukesh krishnan 3531 gold badge6 silver badges20 bronze badges asked Apr 10, 2011 at 16:42 IsraelIsrael 1,2242 gold badges14 silver badges26 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 8

It'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