[英]Pass several variable to a function Jquery
菜鸟问题传入:我使用 jquery function loadNewPicture() 加载图片(显然)和 function 进度(查看上传进度)。
一切正常。
我的问题只涉及我可以传递给 function“progress()”的变量。 这个 function 是我在某个地方拿的模板。 默认情况下,它可以很好地传递变量“e”而不指定它:
myXhr.upload.addEventListener('progress', progress, false);
但我不知道如何传递更多变量。 我想传递变量“类别”所以我期待这样的事情:
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.");
}
}
}
你可以做这样的事情
myXhr.upload.addEventListener('progress', function(event){
progress(event, parameter1, parameter2);
}, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.