jquery函数 - 重新分配全局javascript变量

时间:2011-09-19 04:30:01

标签: javascript jquery uploadify

我正在使用上传文件上传后运行功能的上传脚本。我需要此函数来重新分配javascript变量的值。我需要将“trackid”重新分配给onComplete函数中的值。任何人都可以帮助我吗?

编辑:更多解释......我的页面上有2个上传脚本实例。我需要从第一个上传者那里获取响应并将其作为URL参数分配给第二个上传者。它与document.ready有关吗?我会更新我的代码。

第一个脚本:

<script type="text/javascript">
trackid = 9999999;
</script>
<script type="text/javascript">
        jQuery(document).ready(function() { 

    $('#mainftp').uploadify({
    'uploader'  : 'js/uploadifyposted/uploadify.swf',
    'script'    : 'js/uploadifyposted/uploadify.php?<?php echo urlencode("privateFolderWav=" . $privateFolderWav . "&userid=" . $userid. "&songid=" . $songid);?>',
    'multi'         : true,
    'auto'          : true,
    'height'        :   '32', //height of your browse button file
    'width'         :   '250', //width of your browse button file
    'sizeLimit' :   '51200000',  //remove this to set no limit on upload size
    'simUploadLimit' : '3', //remove this to set no limit on simultaneous uploads
    'buttonImg' : 'img/browse.png',
    'cancelImg' : 'img/cancel.png',
        'folder'    : '<?php echo $privateFolderWav;?>', //folder to save uploads to
        onProgress: function() {
          $('#loader').show();
        },
        onComplete: function(event, queueID, fileObj, response, data, trackid) {
          $('#loader').hide();
          $('#allfiles').load(location.href+" #allfiles>*","");
          $('#filesUploaded').attr('value', ''+response+'');
          trackid = response;
          alert(trackid);

          //location.reload(); //uncomment this line if youw ant to refresh the whole page instead of just the #allfiles div
        }   
    });

    $('ul li:odd').addClass('odd');

}); 

</script>

第二个脚本:

<script type="text/javascript">


        jQuery(document).ready(function() { 

    $('#mainftp2').uploadify({
    'uploader'  : 'js/uploadifymultiple/uploadify.swf',
    'script'    : 'js/uploadifymultiple/uploadify.php?<?php echo urlencode("songid=" . $songid . "&userid=" . $userid . "&trackid=");?>'+trackid,
    'multi'         : true,
    'auto'          : true,
    'height'        :   '32', //height of your browse button file
    'width'         :   '250', //width of your browse button file
    'sizeLimit' :   '51200000',  //remove this to set no limit on upload size
    'simUploadLimit' : '3', //remove this to set no limit on simultaneous uploads
    'buttonImg' : 'img/browse.png',
    'cancelImg' : 'img/cancel.png',
        'folder'    : '<?php echo $multiFolder?>', //folder to save uploads to
        onProgress: function() {
          $('#loader2').show();
        },
        onComplete: function(event, queueID, fileObj, response, data) {
          $('#loader2').hide();
          $('#allfiles2').load(location.href+" #allfiles2>*","");
          $('#filesUploaded2').attr('value', ''+response+'');

          //location.reload(); //uncomment this line if youw ant to refresh the whole page instead of just the #allfiles div
        }   
    });

    $('ul li:odd').addClass('odd');

}); 

</script>

2 个答案:

答案 0 :(得分:1)

我在这段代码中没有看到触发器。看起来两个脚本在加载页面后立即运行,如果是这样,第二个脚本不等待变量重新分配。你应该在第一个

的onComplete中调用第二个函数

答案 1 :(得分:0)

尝试设置:

trackid = 9999999;

而不是

var trackid = 9999999;

var foo ='baa'; &lt; - 私人变量 foo ='baa'&lt; - 全局变量

例如:

`function print2(){     var foo ='baa'; }

print2();警报(FOO); `

不打印'foo'未定。 ` function print2(){     foo ='baa'; }

print2();警报(FOO); ` 打印'baa'