将几个变量传递给函数Jquery

时间:2020-07-01 16:01:48

标签: javascript jquery ajax

Noob的问题即将来临:我使用jquery函数loadNewPicture()加载图片(显然),并使用progress()函数查看以上传百分比表示的成就。

一切正常。

我的问题仅涉及我可以传递给函数“ progress()”的变量。这个功能是我带到某个地方的模板。在默认情况下使用它时,它会很好地传递变量“ e”而不指定它:

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

但是我不知道如何传递更多的变量。 我想传递变量“ category”,所以我期待这样的事情:

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

但是它不起作用。 有想法吗?

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.");
                            }
                        }   
                    }

1 个答案:

答案 0 :(得分:1)

您可以做这样的事情

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