0

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

请按照以下步骤操作:

  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 表单和表格,其中附加了文件输入表单,并且文本输入存储在.hiddenimgdiv 标记中:

<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;   
}
4

1 回答 1

0

如果我要这样做,那么我将添加图像 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 .
});
于 2013-01-24T17:20:36.530 回答