开发者

how to pass js variable as key into options array

开发者 https://www.devze.com 2023-04-12 09:44 出处:网络
I\'m trying to use a variable as the \"key\" value in a javascript swfupload object\'s options array. The receiving page to an ajax request checks the session name/id before continuing p开发者_开发问答

I'm trying to use a variable as the "key" value in a javascript swfupload object's options array. The receiving page to an ajax request checks the session name/id before continuing p开发者_开发问答rocessing, so these values are required.

However, since the session name is variable, I can't just set it statically. I'm trying something like this:

var myid        = ".$php_pageid.";
var sessionid   = '".$php_sessionid."'; 
var sessionname = '".$php_sessionname."';
var swfu = new SWFUpload({
    // Backend Settings
    upload_url: "index.php",
    post_params: 
    {
        "task" : "ajaxUpload",
        "id" : myid, 
        sessionname : sessionid 
    },

However, when I console.dir the swfu object, it shows

sessionname: "234630019cc86c366e1fa390ac737648" 

instead of

d9fb7fb097782697802a63e5f65b765e: "234630019cc86c366e1fa390ac737648"

as is required.

I know I could simply do

var swfu = new SWFUpload({
    // Backend Settings
    upload_url: "index.php",
    post_params: 
    {
        "task" : "ajaxUpload",
        "id" : myid, 
        "'.$php_sessionname.'" : "'.$php_sessionid.'"
    },

But I'm abstracting the js into its own file, and setting all the vars in the PHP file.

Is there a proper way to set a variable as the key of an option pair?


Do it dynamically rather than declaratively:

var params = {
    "task" : "ajaxUpload",
    "id" : myid
};

params[sessionname] = sessionid;

var swfu = new SWFUpload({
    // Backend Settings
    upload_url: "index.php",
    post_params: params,
0

精彩评论

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