1

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

请按照以下步骤操作:

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

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

  3. 现在问题出在这里,在 TABLE ROW 2 中,上传一个文件。您意识到上传完成后它不会在我的表格下方显示文本输入。

所以我的问题是,如果用户在除第一表行中的文件输入之外的任何文件输入中上传文件,为什么它不会显示与这些上传文件关联的文本输入?

下面是显示文件输入以及如何将其附加到下面的 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>

下面是控制upload按钮、上传开始和上传完成时间的代码。

上传按钮处理程序:

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

上传完成:

 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 + '" value="'+imageID+'">Remove</button><br/><hr/></div>');
         }


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

    var buttonid = $(this).attr('value');
    $(this).parent().remove();
     $("#"+ buttonid  +"").remove();
});

      return true;   
}
4

1 回答 1

1

问题出在 stopImageUpload() 函数中。当您加载数据并将其附加到占位符时,您使用当前表单的索引选择要附加到的正确元素(如 listImage)

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 + '" value="'+imageID+'">Remove</button><br/><hr/></div>');
}

但是 hiddenimg 元素只是一个,所以你的选择器找不到任何 dom 元素来追加 new 。你的代码应该是这样的:

if (success == 1){
     result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span>';   
     $('.hiddenimg').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 + '" value="'+imageID+'">Remove</button><br/><hr/></div>');
}

顺便说一句,表单内的表单是不好的做法你可以嵌套 html 表单吗?

于 2013-01-25T02:06:57.537 回答