简体   繁体   English

将几个变量传递给 function Jquery

[英]Pass several variable to a function Jquery

Noob question incoming: Im using the jquery function loadNewPicture() to load picture (obviously) and the function progress() to see the achievement in percent of the upload.菜鸟问题传入:我使用 jquery function loadNewPicture() 加载图片(显然)和 function 进度(查看上传进度)。

Everything is working fine.一切正常。

My question concerns only the variables that I can pass to the function "progress()."我的问题只涉及我可以传递给 function“progress()”的变量。 This function is a template that I took somewhere.这个 function 是我在某个地方拿的模板。 It passes fine the variable "e" without specifiying it when using it by default:默认情况下,它可以很好地传递变量“e”而不指定它:

myXhr.upload.addEventListener('progress', progress, false); myXhr.upload.addEventListener('progress', progress, false);

But I dont know how to pass more variables.但我不知道如何传递更多变量。 I would like to pass the variable "category" so I was expecting something like this:我想传递变量“类别”所以我期待这样的事情:

myXhr.upload.addEventListener('progress', progress(e, category), false); myXhr.upload.addEventListener('progress', progress(e, category), false);

But it does not work.但它不起作用。 Any idea?任何想法?

function loadNewPicture(category, idT)
                    //id pour l'element que l'on va cacher
                    //vari pour les variables a seter du style ?prout="xxx"
                    {
                             
                        var form_data = new FormData();     
                        var ins = document.getElementById(idT).files.length;
                        for (var x = 0; x < ins; x++) 
                        {
                            form_data.append("file[]", document.getElementById(idT).files[x]);
                        }
                        
                        //form_data est une liste. Je rajoute un element a cette liste afin de l'envoyer dans le POST
                        form_data.append('category', category); 
                        
                        $.ajax(
                        {
                            url: 'ajax/saveNewPicture.php', // point to server-side PHP script 
                            dataType: 'text',   // what to expect back from the PHP script, if anything
                            cache: false,
                            contentType: false,
                            processData: false,
                            data: form_data,                                             
                            type: 'post',
                            xhr: function() 
                            //### CA VA PERMETTRE DE SAVOIR LE POURCETAGE DE UPLOAD DES PHOTOS
                            {
                                var myXhr = $.ajaxSettings.xhr();
                                if(myXhr.upload)
                                {
                                    //console.log("CA UPLOAD");
                                    myXhr.upload.addEventListener('progress', progress, false);
                                }
                                return myXhr;
                            },
                            success: function(php_script_response)
                            {
                                $("#presultat" + category).html(php_script_response);
                            },
                            error: function (php_script_response) 
                            {
                                $("#presultat" + category).html(php_script_response);
                            }
                        });
                    }
                    
                    function progress(e, category)
                    //### CETTE FONCTION SERT A AFFICHER LEVOLUTION DE LUPLOAD DES PHOTOS SUR LE SERVEUR.
                    {
                        if(e.lengthComputable)
                        {
                            var max = e.total;
                            var current = e.loaded;

                            var Percentage = (current * 100)/max;
                            $("#presultatDownloadprm").html("UPLOAD : " + Percentage);

                            if(Percentage >= 100)
                            {   
                                $("#presultatDownloadprm").html("UPLOAD REUSSI.");
                            }
                        }   
                    }

You can do something like this你可以做这样的事情

myXhr.upload.addEventListener('progress', function(event){
    progress(event, parameter1, parameter2);
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM