问题描述
- 我在名为validate的函数中声明了一个名为cont的局部变量。
- 我从内部验证函数进程。
- 我发送字符串'cont'作为验证函数的参数。
- 在使用字符串'cont'的进程函数中我想访问javascript局部变量的值喜欢window ['cont']。但我得到了未定义。
- 我想要做的是尝试访问php或$$中的$ GLOBALS等变量。
- I have declared a local variable named cont in a function named validate.
- I am calling a function process from inside validate.
- I am sending the string 'cont' as argument to validate function.
- In the process function using the string 'cont' i want to access the javascript local variable's value like window['cont']. But i get undefined.
- What i am trying to do is trying to access variables like $GLOBALS in php or $$.
这是我做的一个例子。
<script>
function process(str)
{
alert(window[str]);
}
function validate()
{
var cont='once there lived a king named midas';
process('cont')
}
validate();
</script>
原因是我将大部分表格作为ajax。我不想这样做一个请求字符串。
The reason is i do most of the forms as ajax. i dont want to make a request string like this.
var param = "command=insert&content=" + encodeURIComponent(cont);
我想这样做。
var param = makeParam('command,[insert],content,(cont)');
我在makeparam中所做的是使用正则表达式来提取键值对。
所以我从(cont)获得字符串cont并将其替换为窗口变量,如window [cont]。 cont将有字符串'cont'。
what i do in makeparam is i use regular expression to extract key value pairs.so i get the string cont from (cont) and i substitute it into window variable like window[cont]. cont will have the string 'cont'.
那么我们如何通过将变量的名称用作字符串来获取变量的内容?
so how do we get the content of a variable by using the name of the variable as string?
所以我正在寻找javascript相当于php的$$
so i am looking for javascript equivalent of php's $$
已编辑
代码的一部分,我提取内部的cont(续),这意味着我希望字符串的内容在()之间。
a part of the code where i extract cont which is inside (cont) which means i want the content of the string between ().
nxt = str[i+1].match(/\((.*)\)$/)
if(nxt)param += '=' + encodeURIComponent(window[nxt[1]]);
param的内容将是
the content of param would be
"command=insert&content=once there lived a king"
// assume that once there lived a king is encoded
编辑。注2。
经过几次回复,我正在编辑代码以添加此内容。
After few more responses i am editing the code to add this.
I我试图像在PHP中的$ GLOBALS一样。
I am trying to do like $GLOBALS in php.
我还没有尝试过$ GLOBALS是否也会支持局部变量。
I haven't tried whether $GLOBALS would cantain local variables too.
并了解到本地范围不会进入$ GLOBALS。
and learned that local scope will not come into $GLOBALS.
阅读Felix King后更新更新。
我想使用一个函数来构建一个尽可能简单的查询字符串。如下所示。
I want to use a function which will construct a query string as simpler as possible. like the following.
var param = makeParam('command,insert,/title/,/keywords/,/description/,mode,[1],fckcontent,(cont)');
// if it is a text without // or () then the it is a straight key value pair. so i will do comment=insert.
//if it is /title/ then the key is title and its value is an input elements value with id as title so title=getElementById('title')
//if it is mode,[1] then mode is the key and 1 is its direct value//
//if it is fckcontent,(cont) then fckcontent is the key and cont is a javascript local variable which will contain html content from a WYSIWYG editor.
// a sample result will be
var param = "command=insert&keywords=somekeywords&description=somedescription&mode=1&fckcontent=<p>once there lived a king<p>
然后卡萨布兰卡表示$ GlOBALS不会包含本地范围变量和这在javascript中是一样的。那是对的。
and then casablanca stated that $GlOBALS will not contain local scope variables and that is the same way in javascript. that's right.
推荐答案
function validate()
{
var cont='once there lived a king named midas';
process('cont')
}
cont
在函数的本地范围中定义,而不是在全局范围内定义要么只是
cont
is defined in the local scope of the function, not in global scope. Either do just
cont='once there lived a king named midas';
(不含 var
)或
window.cont='once there lived a king named midas';
更新:
但是你为什么要在解析字符串时经历这么多麻烦呢?你为什么不这样做:
But why do you want to go through so much trouble with parsing strings? Why don't you do:
var param = makeParam({command: 'insert', content: encodeURIComponent(cont)});
这篇关于javascript相当于php $$美元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!