最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass an array from one function to another in jQuery - Stack Overflow

programmeradmin0浏览0评论

I have a page with two functions. Function A piles an array and displays a button when done. The user clicks the button and the array is passed into Function B... All I have is Function A:

function createUploader(){
        var fileArray = new Array();
        var i = 0;
        var running = 0;
        var jList = $( "#list" );
            var uploader = new qq.FileUploader({
                element: document.getElementById('uploadDiv'),
                listElement: document.getElementById('separate-list'),
                action: './includes/ajaxUpload/upload.php',
        sizeLimit: 10485760,
        onSubmit: function(id, fileName){
            running++;  
        },
        onComplete: function(id, fileName, responseJSON){
            fileArray[i] = fileName;
            i++;
            running--;
            if(running==0){
            $('#bineBtn').css("display","");
            $.fancybox.resize();
            $('#fancybox-content').width(290);
            $('#fancybox-wrap').width(310);
            $.fancybox.center
            $('.qq-upload-button').width(290);
            }
        }
            });
        }

Is this even possible? What would be the best way to acplish this?

I have a page with two functions. Function A piles an array and displays a button when done. The user clicks the button and the array is passed into Function B... All I have is Function A:

function createUploader(){
        var fileArray = new Array();
        var i = 0;
        var running = 0;
        var jList = $( "#list" );
            var uploader = new qq.FileUploader({
                element: document.getElementById('uploadDiv'),
                listElement: document.getElementById('separate-list'),
                action: './includes/ajaxUpload/upload.php',
        sizeLimit: 10485760,
        onSubmit: function(id, fileName){
            running++;  
        },
        onComplete: function(id, fileName, responseJSON){
            fileArray[i] = fileName;
            i++;
            running--;
            if(running==0){
            $('#bineBtn').css("display","");
            $.fancybox.resize();
            $('#fancybox-content').width(290);
            $('#fancybox-wrap').width(310);
            $.fancybox.center
            $('.qq-upload-button').width(290);
            }
        }
            });
        }

Is this even possible? What would be the best way to acplish this?

Share Improve this question asked Mar 16, 2011 at 16:16 jreed121jreed121 2,0975 gold badges35 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Just declare the array outside of the functions, and it can be accessed by them both.

var myarray = [];
function foo(val) {
    myarray.push(val);
}

function bar() {
   alert (myarray);
}

Further reading: http://www.digital-web./articles/scope_in_javascript/

When creating the button in Function A, could you not do the following:

function function_a()
{
    var theArray = [1, 2, 3, 4];
    var theButton = $('<button>Click Me</button>');
    theButton.click(function() { function_b(theArray) });
}

function function_b(myArray)
{
    // Run function code here...
}

You can serialize array to json, save it in value attribute in hidden field, and when button is clicked read hidden field value in function B and deserialize JSON.

发布评论

评论列表(0)

  1. 暂无评论