使用除第一行以外的任何表行中的文件输入上载文件时,不会显示文本输入

时间:2013-01-22 17:52:26

标签: jquery html iframe

我在这里有一个申请:APPLICATION

请按照以下步骤操作:

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

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

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

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

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

    下面是控制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;   
    }
    

1 个答案:

答案 0 :(得分: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元素来附加新元素。 你的代码应该是这样的:

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

顺便说一句,表单里面的表单是不好的做法Can you nest html forms?