开发者

JSON Variable showing as text

I am not too savvy with JSON so any help would be great...

I am l开发者_开发技巧oading a page via JQuery's .load.

I am setting varname as a variable, but it is posting as the actual variable name not what I set it to.

In other words it is showing up as "varname" instead of "class_id".

var varname = "class_id";

$(this).siblings(".container").load(loadurl,{varname:openedid,"all_ids":allids});

Firebug Screenshot


It's because (AFAIK) you cannot put expression as an ID in the hash constructor, because they're automatically quoted as strings.

In other words, this is valid JavaScript

$(this).load(url, {name: "Foo", age: 13});

The keys name and age are there. In JavaScript you don't have to quote the keys of a hash, although in strict JSON the quoting is necessary for keys. (But the quoting wouldn't hurt. And in some cases, as Jordan suggested in the comments, it's necessary e.g. you want to use reserved words like var as the hash key)

To accomplish your desired effect, I would suggest a lengthy solution (any clever one-liner anyone?)

var varname = "class_id";

var data = {all_ids: allids};
data[varname] = openedid;

$(this).siblings('.container').load(loadurl, data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜