【问题标题】:How to remove correct text input depending on while file is removed如何根据删除文件的时间删除正确的文本输入
【发布时间】:2013-01-24 17:04:11
【问题描述】:

我在这里有一个应用程序:APPLICATION

请按以下步骤操作:

  1. 单击Add Question 按钮将文件输入附加到下表中:

  2. 通过使用文件输入上传 2 张图片(一次一张),每次成功上传一张图片时,它会在表格下方和下方显示上传文件的名称,并在文本输入中显示其 id .

  3. 现在问题出在这里,单击您上传的第二个文件名的Remove 按钮,您将看到它成功删除了关联的文件名和删除按钮,但它删除了下面的两个文本输入。这是不正确的,它应该只删除 id 与已删除文件名相关联的文本输入。

这就是我的问题,当用户单击Remove 按钮时,如何才能仅删除与已删除文件名关联的文本输入,而不是在单击删除按钮时删除所有文本输入?

下面的代码显示了文件输入以及如何将其附加到下面的 html 表中,以及显示存储文本输入的.hiddenimg div:

jquery附加文件输入表单:

function GetFormImageCount(){ 
  return $('.imageuploadform').length + 1;
}

function insertQuestion(form) {



    var $tbody = $('#qandatbl_onthefly > tbody');
    var $tr = $("<tr class='optionAndAnswer' align='center'>");
    var $image = $("<td width='17%' class='image'></td>");

    var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target_image' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" +
        "<p class='imagef1_upload_form'><label>" +
        "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
        "<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
        "<p class='imagef1_upload_process'>Loading...<br/><img src='Images/loader.gif' /></p>" +
        "<input type='hidden' class='numimage' name='numimage' value='" + GetFormImageCount() + "' />" +
        "</p><p class='imagemsg'></p><p class='listImage'></p>" +
        "<iframe class='upload_target_image' name='upload_target_image' src='/' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>");

    $image.append($fileImage);



    $tr.append($image);
    $tbody.append($tr);


}

HTML 表单和表格,其中附加了文件输入表单以及文本输入存储在 .hiddenimg div 标签中的位置:

<form id="QandA" action="insertQuestion.php" method="post">


<table id="questionBtn" align="center">
<tr>
<th>
<input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
</th>
</tr>
</table>

</div>
<hr/>

<table id="qandatbl" align="center" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
    <th width="17%" class="image">Image</th>
</tr>
</thead>
</table>
<div id="qandatbl_onthefly_container">
<table id="qandatbl_onthefly" align="center" cellpadding="0" cellspacing="0" border="0">
<tbody>
</tbody>
</table>
</div>

<div class="hiddenimg"><!-- All uploaded image file ids go here --></div>

</form>

下面是显示文件上传处理程序的代码,启动文件上传的函数,重要的是停止文件上传的函数,它控制文件名、文本输入和删除按钮的显示以及控制删除的位置当Remove 按钮被点击时:

上传按钮处理程序:

  function imageClickHandler(imageuploadform){ 
  if(imageValidation(imageuploadform)){ 
      window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform); 
      return startImageUpload(imageuploadform); 
  } 
  return false;

开始上传:

function startImageUpload(imageuploadform){

  $(imageuploadform).find('.imagef1_upload_process').show()
  $(imageuploadform).find('.imagef1_upload_form').hide();
  $(imageuploadform).find('.imagemsg').hide();
  sourceImageForm = imageuploadform;

      return true;
}

上传完成:

 var imagecounter = 0;

function stopImageUpload(success, imageID, imagefilename){

      var result = '';
      imagecounter++;

      if (success == 1){
         result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span>';   
            $('.hiddenimg').eq(window.lastUploadImageIndex).append('<input type="text" name="imgid[]" id="'+imageID+'" value="' + imageID + '" />');
            $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" data-imageID="'+imageID+'"  data-image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
         }

      $(sourceImageForm).find('.imagef1_upload_process').hide();
      $(sourceImageForm).find('.imagemsg').html(result);
      $(sourceImageForm).find('.imagemsg').show();
      $(sourceImageForm).find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
      $(sourceImageForm).find('.imagef1_upload_form').show();


  var _imagecounter = imagecounter;

$('.listImage').eq(window.lastUploadImageIndex).find(".deletefileimage").on("click", function(event) {
    jQuery.ajax("deleteimage.php?imagefilename=" + $(this).attr('data-image_file_name')).done(function(data) {
        $(".imagemsg" + _imagecounter).html(data);
    });

    $(this).parent().remove();
    $(".hiddenimg input").remove();
});

      return true;   
}

【问题讨论】:

    标签: jquery html ajax upload


    【解决方案1】:

    如果我要这样做,那么我会 添加图像 id 作为按钮的值,然后检索它,并删除关联的输入

    $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename)
     + '<button type="button" class="deletefileimage" data-imageID="'+imageID+'"  data-image_file_name="' + imagefilename + '"  value="'+imageID+'">Remove</button>
    <br/>
    <hr/>
    </div>')
    

    然后我会将其添加为文本输入的 id,甚至添加到名称或类或其他任何内容中

    $('.listImage').eq(window.lastUploadImageIndex).find(".deletefileimage").on("click", function(event) {
        jQuery.ajax("deleteimage.php?imagefilename=" + $(this).attr('data-image_file_name')).done(function(data) {
            $(".imagemsg" + _imagecounter).html(data);
        });
    var buttonid = $(this).attr(value) // or class or what ever
        $(this).parent().remove();
        $("#"+ buttonid  +"").remove(); // this will be the id of the text input (you allready have that in your application .
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 2015-07-06
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多