Javascript AJAX-为每个上传的文件调用函数

Javascript AJAX-为每个上传的文件调用函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用AJAX上传文件,我有以下问题。上载文件时,将为每个上载的文件调用*handleSuccess*函数。这给我带来了一个问题——当你们点击图片链接时,链接被插入到文本区域。在我的例子中,对于每个文件,插入相同数量的链接。我怎样才能只调用一次函数,或者删除所有创建的对象,只得到一个对象(或者基本上可以防止这种情况的任何东西)? 代码: 函数img\u上传(url){ { var fileTemplate=“”; fileTemplate+=“”; fileTemplate+=“{{filena

我使用AJAX上传文件,我有以下问题。上载文件时,将为每个上载的文件调用*handleSuccess*函数。这给我带来了一个问题——当你们点击图片链接时,链接被插入到文本区域。在我的例子中,对于每个文件,插入相同数量的链接。我怎样才能只调用一次函数,或者删除所有创建的对象,只得到一个对象(或者基本上可以防止这种情况的任何东西)? 代码:

函数img\u上传(url){
{
var fileTemplate=“”;
fileTemplate+=“”;
fileTemplate+=“{{filename}}”;
fileTemplate+=“”;
fileTemplate+=“”;
函数slugify(文本){
text=text.替换(/[^-a-zA-Z0-9,&\s]+/ig');
text=text.replace(/-/gi,“”);
text=text.replace(/\s/gi,“-”);
返回文本;
}
$(“#dropbox”).HTML5上传程序({
姿势:url,
OnClient LoadStart:函数(e,文件){
var upload=$(“#upload”);
if(upload.is(“:hidden”)){
upload.show();
}
upload.append(fileTemplate.replace(/{{id}}/g,slugify(file.name)).replace(/{{filename}/g,file.name))
},
onClientLoad:函数(e,文件){
$(“#”+slugify(file.name))
.find(“.preview”)
.附加(“”);
},
onServerLoad:函数(e,文件){
},
onSuccess:函数(e、文件、数据){
var id\u slika=slugify(file.name)
上传文件夹=$(“#上传”);
上传文件夹.find(“#”+id\u slika.find('a').attr('data-id',data);
var img_delete=$('.image_delete_tmp');
handleSuccess(上传文件夹);
删除图像(img_删除“tmp”);
}
});
}
}
函数handleSuccess(){
var upload_folder=$('#upload');
log(upload_folder.first());
上传文件夹('click','img',函数(e){
变量$this=$(this),
text=$this.parent().parent().find('a').data('id'),
img=('pages/'+text+'“/>');
$(“#第#页"srb”)。插入器(img);
//元素。移除();
返回false;
});
}
这是我点击文件时得到的(这是3个文件):


如果这个插件支持onSuccessAll之类的东西,那就简单了……但从外观上看,它不支持……如果只是显示一个图像,我会:

function handleSuccess() {
  var upload_folder = $('#upload');
  console.log(upload_folder.first());
  upload_folder.on('click', 'img', function(e){

    if ($(".onlyone").length < 1){
        var $this = $(this),
        text = $this.parent().parent().find('a').data('id'),
        img = ('<img class="onlyone" src="<?php echo IMG ?>pages/' +  text +  '" />');
        $("#page_srb").insertAtCursor(img);
        // element.remove();
        return false;
    }
});
}
函数handleSuccess(){
var upload_folder=$('#upload');
log(upload_folder.first());
上传文件夹('click','img',函数(e){
如果($(“.onlyone”)。长度<1){
变量$this=$(this),
text=$this.parent().parent().find('a').data('id'),
img=('pages/'+text+'“/>');
$(“#第#页"srb”)。插入器(img);
//元素。移除();
返回false;
}
});
}

因此,它会检查是否已经有类为onlyone的元素,并且只有在没有的情况下才会添加image。但是,这意味着图像将在第一次上传时显示,而不是最后一次。

我仍然得到相同的结果。我想我必须找到插入onSuccessAll功能的方法。这很奇怪。。。您是否确实重写了原始函数?。。。。不管怎么说,我自己也是为了同样的目的。非常易于使用和定制。。
<img src="http://localhost/pirot_opstina/public/img/pages/Digital_composite_spring_(9)37.jpg" />
    <img src="http://localhost/pirot_opstina/public/img/pages/Digital_composite_spring_(9)37.jpg" />
    <img src="http://localhost/pirot_opstina/public/img/pages/Digital_composite_spring_(9)37.jpg" />
function handleSuccess() {
  var upload_folder = $('#upload');
  console.log(upload_folder.first());
  upload_folder.on('click', 'img', function(e){

    if ($(".onlyone").length < 1){
        var $this = $(this),
        text = $this.parent().parent().find('a').data('id'),
        img = ('<img class="onlyone" src="<?php echo IMG ?>pages/' +  text +  '" />');
        $("#page_srb").insertAtCursor(img);
        // element.remove();
        return false;
    }
});
}