I have a button:
我有一个按钮:
<button class="btn btn-info continue">
<i class="ace-icon fa fa-check bigger-110"></i>
Continue
</button>
Onclick:
$(".continue").click(function(e) {
currForm = $(this).parents('form');
e.preventDefault();
});
I can get the id pretty easily: currForm.attr('id');
, but can I set the value of this id as a variable.
我很容易得到id:currForm.attr('id');,但是我可以将这个id的值设置为变量。
Something like php's variable variables:
像php的变量变量:
$a = 'hello';
$$a = 'world';
echo $hello;
Edit: I don't want to change element's id. I want to get this ID and use it as a name for a javascript variable. For example, the element I provided above is in a form
that has ID='example_id'. currForm.attr('id')
will give me example_id
and I want to set a variable example_id
and assign a value to it.
编辑:我不想更改元素的ID。我想获取此ID并将其用作javascript变量的名称。例如,我在上面提供的元素是具有ID ='example_id'的形式。 currForm.attr('id')会给我一个example_id,我想设置一个变量example_id并为其赋值。
var example_id = 'some value';
var example_id ='some value';
6 个解决方案
#1
6
Here 3 options for you.
这里有3个选项。
eval
(not recommended)
You can use the Javascript function eval
to achieve what you want. But be aware, this in not recommended (I emphasized it twice, hope you understood!).
您可以使用Javascript函数eval来实现您想要的效果。但要注意,这不推荐(我强调了两次,希望你理解!)。
Don't use eval needlessly!
eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.
eval()是一个危险的函数,它执行它以调用者的特权传递的代码。如果您使用可能受恶意方影响的字符串运行eval(),您最终可能会使用您的网页/扩展程序的权限在用户的计算机上运行恶意代码。更重要的是,第三方代码可以看到调用eval()的范围,这可能导致类似函数不受影响的方式的可能攻击。
It would be used like that :
它会像那样使用:
eval('var ' + varName + ' = "something";');
Window object notation (Better than eval
, still not really recommended)
This method consist of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overrided by other JS files which is bad. If you want to know more on that subject, this is a good question : Storing a variable in the JavaScript 'window' object is a proper way to use that object?.
此方法包括在全局窗口对象上使用JavaScript提供的对象表示法。这会污染全局窗口范围,并且可以被其他不良的JS文件覆盖。如果你想了解更多有关该主题的信息,这是一个很好的问题:在JavaScript'window'对象中存储变量是使用该对象的正确方法吗?
To use that method, you would do thing like that :
要使用该方法,您可以这样做:
window[varName] = something;
alert(window[varName]);
Using an object (recommended)
The best solution would be to creating you own variable scope. For instance, you could create on the global scope a variable and assign an object to it. You can then use the object notation to create you dynamics variables. It work the same way as the window does :
最好的解决方案是创建自己的变量范围。例如,您可以在全局范围内创建变量并为其分配对象。然后,您可以使用对象表示法创建动态变量。它的工作方式与窗口相同:
var globalScope = {};
function awesome(){
var localScope = {};
globalScope[varName] = 'something';
localScope[varName] = 'else';
notSoAwesome();
}
function notSoAwesome(){
alert(globalScope[varName]); // 'something';
alert(localScope[varName]); // undefined
}
#2
2
You can do it using javascript object:
你可以使用javascript对象来做到这一点:
var currForm = $(this).parents('form');//form id="hello"
var obj = {};
obj[currForm.attr('id')] = 30;
console.log(obj.hello)
#3
1
You can use and object to store your variables in:
您可以使用和对象将变量存储在:
var variables = {};
to add a variable just type:
添加变量只需输入:
variables[name] = value;
and to access the value:
并访问该值:
variables[name]
Check it out here: jsFiddle
在这里查看:jsFiddle
Button 2 reads the value of variables[formid] and button 1 sets formid to submitted
按钮2读取变量[formid]的值,按钮1将formid设置为提交
#4
0
Try one of the following:
请尝试以下方法之一:
$(this).attr(example_id, "value")
Or
window[ currForm.attr(id)] = 12;
Or
window[ this.id ] = 12;
#5
0
Your best option for archieving what you really want is using eval(), like this:
你最好的选择是使用eval(),就像这样:
eval("var " + currForm.attr('id');
Check this link ->
检查此链接 - >
Dynamic Variables in Javascript
Javascript中的动态变量
#6
-1
Use eval (but not recommended):
使用eval(但不推荐):
var id = 'that_id';
eval(id+" = 'other id';");
alert(that_id); // results: 'other id'
#1
6
Here 3 options for you.
这里有3个选项。
eval
(not recommended)
You can use the Javascript function eval
to achieve what you want. But be aware, this in not recommended (I emphasized it twice, hope you understood!).
您可以使用Javascript函数eval来实现您想要的效果。但要注意,这不推荐(我强调了两次,希望你理解!)。
Don't use eval needlessly!
eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.
eval()是一个危险的函数,它执行它以调用者的特权传递的代码。如果您使用可能受恶意方影响的字符串运行eval(),您最终可能会使用您的网页/扩展程序的权限在用户的计算机上运行恶意代码。更重要的是,第三方代码可以看到调用eval()的范围,这可能导致类似函数不受影响的方式的可能攻击。
It would be used like that :
它会像那样使用:
eval('var ' + varName + ' = "something";');
Window object notation (Better than eval
, still not really recommended)
This method consist of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overrided by other JS files which is bad. If you want to know more on that subject, this is a good question : Storing a variable in the JavaScript 'window' object is a proper way to use that object?.
此方法包括在全局窗口对象上使用JavaScript提供的对象表示法。这会污染全局窗口范围,并且可以被其他不良的JS文件覆盖。如果你想了解更多有关该主题的信息,这是一个很好的问题:在JavaScript'window'对象中存储变量是使用该对象的正确方法吗?
To use that method, you would do thing like that :
要使用该方法,您可以这样做:
window[varName] = something;
alert(window[varName]);
Using an object (recommended)
The best solution would be to creating you own variable scope. For instance, you could create on the global scope a variable and assign an object to it. You can then use the object notation to create you dynamics variables. It work the same way as the window does :
最好的解决方案是创建自己的变量范围。例如,您可以在全局范围内创建变量并为其分配对象。然后,您可以使用对象表示法创建动态变量。它的工作方式与窗口相同:
var globalScope = {};
function awesome(){
var localScope = {};
globalScope[varName] = 'something';
localScope[varName] = 'else';
notSoAwesome();
}
function notSoAwesome(){
alert(globalScope[varName]); // 'something';
alert(localScope[varName]); // undefined
}
#2
2
You can do it using javascript object:
你可以使用javascript对象来做到这一点:
var currForm = $(this).parents('form');//form id="hello"
var obj = {};
obj[currForm.attr('id')] = 30;
console.log(obj.hello)
#3
1
You can use and object to store your variables in:
您可以使用和对象将变量存储在:
var variables = {};
to add a variable just type:
添加变量只需输入:
variables[name] = value;
and to access the value:
并访问该值:
variables[name]
Check it out here: jsFiddle
在这里查看:jsFiddle
Button 2 reads the value of variables[formid] and button 1 sets formid to submitted
按钮2读取变量[formid]的值,按钮1将formid设置为提交
#4
0
Try one of the following:
请尝试以下方法之一:
$(this).attr(example_id, "value")
Or
window[ currForm.attr(id)] = 12;
Or
window[ this.id ] = 12;
#5
0
Your best option for archieving what you really want is using eval(), like this:
你最好的选择是使用eval(),就像这样:
eval("var " + currForm.attr('id');
Check this link ->
检查此链接 - >
Dynamic Variables in Javascript
Javascript中的动态变量
#6
-1
Use eval (but not recommended):
使用eval(但不推荐):
var id = 'that_id';
eval(id+" = 'other id';");
alert(that_id); // results: 'other id'