隐藏的输入被覆盖或删除

时间:2012-09-25 22:53:06

标签: javascript jquery

基本上以下代码导致了问题:

    var imagecounter = 0;

function stopImageUpload(success, imagefilename){

      var result = '';
      imagecounter++;

      if (success == 1){
         result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span><br/><br/>';      
         $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>'); 
      }
      else if (success == 2){
          result = '<span class="imagemsg'+imagecounter+'"> The file upload was canceled</span><br/><br/>';
      }
      else {
         result = '<span class="imagemsg'+imagecounter+'">There was an error during file upload</span><br/><br/>';
      }

      $(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
      $(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
//THIS LINE BELOW
      $(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" class="fileImage" type="file"/></label><br/><br/><label><input type="submit" name="submitImageBtn" class="sbtnimage" value="Upload" /></label><label><input type="button" name="imageClear" class="imageClear" value="Clear File"/></label>');
//THIS LINE ABOVE
      $(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');

  var _imagecounter = imagecounter;

      return true;   
}

以上代码取自成功/错误上传发生后运行的代码。此代码继续覆盖负责上载图像的表单。现在这对成功来说很好,因为接口为每个图像使用单独的表单...一旦你获得成功,你就不会再次重用那个表单(因此服务器端没有错误用于后续上传)。

然而,当图片上传错误或取消时,我尝试重复使用该表单再次上传图片?如果是这样,你已经覆盖并删除了numimage输入html:

<input type='hidden' class='numimage' name='numimage' value='" numimage "' />  

这意味着我会收到错误,因为此字段不再存在。

我的问题是我该如何解决这个问题?如何解决问题,以便我不会丢失隐藏输入表单中的值并且该字段仍然存在?

以下是上传文件的完整表格(每次用户点击按钮时,每个表单都会附加到表格中,因此可能有多个表单,每行一个表单:

  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_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='imagef1_upload_form' align='center'><br/><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>" + 
    "<input type='hidden' class='numimage' name='numimage' value='" + numimage + "' />" +
    "</p><p class='imagef1_cancel' align='center'><label>" +
    "<input type='reset' name='imageCancel' class='imageCancel' value='Cancel' /></label>" +
    "</p><p class='listImage' align='left'></p>" +
    "<iframe class='upload_target_image' name='upload_target_image' src='#' style='width:300px;height:300px;border:0px;solid;#fff;'></iframe></form>");     

    $image.append($fileImage);

1 个答案:

答案 0 :(得分:1)

两个最明显的选择是:

  • 替换表单时,请在新内容中包含隐藏字段。

  • 请勿替换表单,只需将结果放入现有元素中。

在表单中放置一个空元素:

...<p class='imagef1_upload_form' align='center'><br/><span class='msg'></span><label>...

然后将结果放在元素中:

$(sourceImageForm).find('.imagef1_upload_form .msg').html(result);