In the top of my Jquery I've got many variables (associated with values) called : files_1, files_2, etc.
在我的Jquery的顶部,我有许多变量(与值相关联),称为:files_1,files_2等。
They are created in a script, in the bottom of my page :
它们是在我的页面底部的脚本中创建的:
<script>
$(function () {
<?php foreach ($folders as $f) { ?>
var files_<?=$f['request_id']?> = 0;
<?php } ?>
…
});
</script>
In my html I've got a link like :
在我的HTML中,我有一个像这样的链接:
<a href="#" class="delete" data-request-id="2" title="delete">Delete</a>
data-request-id parameter gives me a number, the ones you've got in my variables names on top. In my example, it's data-request-id="2" : files_2.
data-request-id参数给出了一个数字,你在我的变量名称中得到的数字。在我的示例中,它是data-request-id =“2”:files_2。
Next, I've got a Jquery function that catch data values from links :
接下来,我有一个Jquery函数来捕获链接中的数据值:
$('.request-files').on('click', 'a.delete', function (e) {
e.preventDefault();
var $link = $(this);
var $id = $link.data('request-id');
console.log(files_$id); // <-- It doesn't work
});
What I need to do is to retrieve the value of the variables files_x. In my example, I tried to get them using files_$id but it doesn't work.
我需要做的是检索变量files_x的值。在我的示例中,我尝试使用files_ $ id来获取它们,但它不起作用。
Any idea ?
任何的想法 ?
1 个解决方案
#1
2
If you have your variables defined in the global scrope, they are attached to the window
object. So you should be able to access your variables by using bracket notation on the window object:
如果在全局scrope中定义了变量,则它们将附加到窗口对象。因此,您应该能够通过在窗口对象上使用括号表示法来访问变量:
$('.request-files').on('click', 'a.delete', function (e) {
e.preventDefault();
var $link = $(this);
var $id = $link.data('request-id');
console.log(window['files_' + $id]); // <-- It DOES work
});
UPD: your variables are in the closure of document.ready function ($(function() {...});
), so you won't be able to access them from other scopes. I assume that your click handler is within that closure as well. I can suggest creating a separate object with properties named file_<ID>
- it will work much alike as with window:
UPD:您的变量在document.ready函数($(function(){...});)的闭包中,因此您将无法从其他范围访问它们。我假设您的点击处理程序也在该闭包内。我可以建议使用名为file_
<script>
$(function () {
var filesMap = {};
<?php foreach ($folders as $f) { ?>
filesMap['files_' + <?=$f['request_id']?>] = 0;
<?php } ?>
…
$('.request-files').on('click', 'a.delete', function (e) {
e.preventDefault();
var $link = $(this);
var $id = $link.data('request-id');
console.log(filesMap['files_' + $id]);
});
});
</script>
I am not familiar with PHP, so the string concatenation in request_id
part might be different, but the logic remains.
我不熟悉PHP,因此request_id部分中的字符串连接可能不同,但逻辑仍然存在。
#1
2
If you have your variables defined in the global scrope, they are attached to the window
object. So you should be able to access your variables by using bracket notation on the window object:
如果在全局scrope中定义了变量,则它们将附加到窗口对象。因此,您应该能够通过在窗口对象上使用括号表示法来访问变量:
$('.request-files').on('click', 'a.delete', function (e) {
e.preventDefault();
var $link = $(this);
var $id = $link.data('request-id');
console.log(window['files_' + $id]); // <-- It DOES work
});
UPD: your variables are in the closure of document.ready function ($(function() {...});
), so you won't be able to access them from other scopes. I assume that your click handler is within that closure as well. I can suggest creating a separate object with properties named file_<ID>
- it will work much alike as with window:
UPD:您的变量在document.ready函数($(function(){...});)的闭包中,因此您将无法从其他范围访问它们。我假设您的点击处理程序也在该闭包内。我可以建议使用名为file_
<script>
$(function () {
var filesMap = {};
<?php foreach ($folders as $f) { ?>
filesMap['files_' + <?=$f['request_id']?>] = 0;
<?php } ?>
…
$('.request-files').on('click', 'a.delete', function (e) {
e.preventDefault();
var $link = $(this);
var $id = $link.data('request-id');
console.log(filesMap['files_' + $id]);
});
});
</script>
I am not familiar with PHP, so the string concatenation in request_id
part might be different, but the logic remains.
我不熟悉PHP,因此request_id部分中的字符串连接可能不同,但逻辑仍然存在。