最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - JavaScript literal, pointer value for key - Stack Overflow

programmeradmin0浏览0评论

I have the following code:

name= "a";
value="b"
$.post("ajax.php", {name:value})....

However ajax.php will receive name=b instead of a=b.

How would I make it receive the latter?

I have the following code:

name= "a";
value="b"
$.post("ajax.php", {name:value})....

However ajax.php will receive name=b instead of a=b.

How would I make it receive the latter?

Share Improve this question edited Nov 26, 2011 at 19:02 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Nov 26, 2011 at 18:04 TarangTarang 76k39 gold badges219 silver badges279 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 8
var obj = {};
obj[name] = value;
$.post("ajax.php", obj)...

To get the effect you desire you can do the following...

name = "a";
value = "b";
var values = {};
values[name] = value;
$.post("ajax.php", values);

The name here is not treated as to be replaceable ...

you should manually write :

{a:"b"}
name= "a";
value="b";

var o = {};
o[name]= value;

$.post("ajax.php", o);

You can't use a variable for key element (if you consider using literal declaration). It has to be static

You need to do this:

$.post("ajax.php", {"a":value})

You may be able to build the object programmably though. You can create an object in this way:

var obj = {};
obj["a"] = "b";

So try this:

var obj = {};
obj[name] = value;

And pass that to the post call...

发布评论

评论列表(0)

  1. 暂无评论