I have a variable var commision_1 = 2000 ;var commision_2 = 4000;
and a function:
function updateCommission(period){
$("input:text[name='commission']").val('commision_'+period);
}
I would like if period is 1 then the value of the textbox be 2000; I know I can use arrays, but I am working on someone else's code, is there a way around it just like in php how I would have written $x = 'commision_' .$period; echo $$x;
$x = 'commision_' .$period; echo $$x;
You can use square bracket notation to get variable variables.
Provided these variables are defined in the window scope:
function updateCommission(period){
$("input:text[name='commission']").val(window['commision_' + period]);
}
Having said that, defining numbered variables ( all the way up to 2000..!? ) isn't a good idea. An array was born to serve this purpose. My best answer would be to request these variables be re-written as an array, but I appreciate this is often easier said than done.
var commission_1 = 2000, commission_2 = 4000;
var name = 'commission_1';
console.log(window[name]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.