开发者

jQuery param() for associative array with values assigned at runtime

开发者 https://www.devze.com 2023-01-11 18:42 出处:网络
I\'m trying to serialize to URL string an associative array whose elements are defined at runtime in the form of arr[key]=value, where key and value are determined at runtime, but arr is predefined as

I'm trying to serialize to URL string an associative array whose elements are defined at runtime in the form of arr[key]=value, where key and value are determined at runtime, but arr is predefined as a global before runtime. I can't get the param() function to work with this kind of array. Is there another funct开发者_如何学Cion to use to serialize such arrays?

var arr=[];
arr["apple"]="poisoned";
arr["banana"]="digested";

var str=jQuery.param(arr);
$("#results").text(str);
​


Use object notation instead:

var arr={};
arr["apple"]="poisoned"; // or arr.apple
arr["banana"]="digested"; // or arr.banana

While objects and arrays operate in a similar same way in JS its still better to think of arrays as being numerically indexed arrays, as opposed to hashes.

0

精彩评论

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