我在这里有一个应用程序,用户可以在其中添加一行并显示文件输入。
尝试清除文件输入时出现 2 个问题:
问题 1:如果我在应用程序中添加 2 行,如果我填写两个文件输入并单击其中一行中的“单击文件”按钮,则不是清除行内的文件,而是清除所有文件不正确的行,它应该清除我想要清除文件的行内的文件。
问题2:点击“上传”按钮上传文件后,上传成功与否后,如果我选择另一个文件,然后点击“清除文件”尝试清除,则无法清除文件。
如何解决这两个问题:
<script type="text/javascript">
var sourceImageForm;
function insertQuestion(form) {
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $image = $("<td class='image'></td>");
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(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/><label class='imagelbl'>" +
"(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label>" +
"<input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>" +
"<label><input type='button' name='imageClear' class='imageClear' value='Clear File'/></label>" +
"</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px;solclass #fff;'></iframe></form>");
$image.append($fileImage);
$tr.append($image);
$tbody.append($tr);
$(".imageClear").click(function(event){
event.preventDefault();
$(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
});
}
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
$(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
sourceImageForm = imageuploadform;
return true;
}
function stopImageUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
$(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" type="file"/></label><br/><label>(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /></label><label><input type="button" name="imageClear" class="imageClear" value="Clear File"/></label>');
$(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');
return true;
}
</script>