全球及JavaScript 中的局部变量
我有一个变量和两个函数。两者都使用该变量。第一个函数是在每次使用变量值时(全局)更改变量值。这就是我想要的,但它不适合我。
x = 1;
function f1()
{
x = x + 1;
// use x
}
function f2()
{
// use x
}
我读过其他线程,但 x 始终为 1,这非常令人沮丧:|
添加:实际代码
<script type="text/javascript">
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
return (S4() + S4() + ";" + S4() + ";" + S4() + ";" + S4() + ";" + S4() + S4() + S4());
}
P = '';
function Save() {
P = guid();
$('#btnBrowse').uploadifyUpload();
}
$(document).ready(function () {
$('#txtText').elastic();
$('#btnBrowse').uploadify({
'uploader': '../uploadify.swf',
'script': '../uploadify.ashx',
'cancelImg': '/uploadify/cancel.png',
'folder': '../images/Albums/',
'multi': true,
'fileDesc': 'Web Image Files (.JPG, .GIF, .PNG)',
'fileExt': '*.jpg;*.gif;*.png',
'scriptData': { 'Album_ID': P },
'buttonText': 'Upload Images'
});
});
</script>
,因此变量是 P 。它由 jquery 函数(uploadify)使用。每次我执行 保存函数我希望获得变量 P 的新值。但总是一样的??
I have one variable and two functions . The variable is used by both. and the first function is changing the variable value (globally) each time it's used by it . This is what I want but it is not working with me .
x = 1;
function f1()
{
x = x + 1;
// use x
}
function f2()
{
// use x
}
I've read other threads but x is always 1 which is very frustrating :|
added: actual code
<script type="text/javascript">
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
return (S4() + S4() + ";" + S4() + ";" + S4() + ";" + S4() + ";" + S4() + S4() + S4());
}
P = '';
function Save() {
P = guid();
$('#btnBrowse').uploadifyUpload();
}
$(document).ready(function () {
$('#txtText').elastic();
$('#btnBrowse').uploadify({
'uploader': '../uploadify.swf',
'script': '../uploadify.ashx',
'cancelImg': '/uploadify/cancel.png',
'folder': '../images/Albums/',
'multi': true,
'fileDesc': 'Web Image Files (.JPG, .GIF, .PNG)',
'fileExt': '*.jpg;*.gif;*.png',
'scriptData': { 'Album_ID': P },
'buttonText': 'Upload Images'
});
});
</script>
so the variable is P . and it is used by jquery function (uploadify) . each time I excute
Save function I expect I get a new value for variable P . But is always the same ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是执行代码的时间。 uplodify 选项在页面加载时设置(其中包括在页面加载时传递
P
),并且由于P
是一个字符串,因此更改P
稍后(通过save()
)不会更改您传递的值。您可以通过传递对对象的引用作为选项而不是字符串来解决此问题编辑:不起作用。该插件提供了
uploadifySettings
[docs]更改设置的方法上传实例。用它来更新scriptData
设置:The problem is the time when you execute the code. The uplodify options are set on page load (which includes that
P
is passed on page load) and asP
is a string, changingP
later (throughsave()
) will not change the value you passed.You can solve this by passing a reference to the object as option, instead of the stringEdit: Didn't work.The plugin provides a
uploadifySettings
[docs] method to change the settings of an uploadify instance. Use it to update thescriptData
settings:也许这个小提琴可以帮助您更好地理解全局范围: http://jsfiddle.net/XFx27/1/< /a>
函数 function funcName() 之后缺少括号 ()
Maybe this fiddle can help you understand global scope a little better: http://jsfiddle.net/XFx27/1/
Your missing parens () after your functions function funcName()
首先,f1函数应该是:
我在chrome控制台中尝试了代码,我认为它确实有效。
firstly, the f1 function should be:
I tried the code in chrome console and I think it really works.