私はここにアプリケーションを持っています:アプリケーション
以下の手順に従ってください。
ボタンをクリックして
Add Question
、ファイル入力を下の表に追加します。ファイル入力アップロード2つの画像を(一度に1つずつ)使用することにより、画像が正常にアップロードされるたびに、アップロードされたファイルの名前がテーブルの下と下に表示され、テキスト入力でそのIDが表示されます。
ここで問題が発生しました
Remove
。アップロードした2番目のファイル名のボタンをクリックすると、関連付けられたファイル名と削除ボタンが正常に削除されますが、下にある両方のテキスト入力が削除されます。これは正しくありません。削除されたファイル名にIDが関連付けられているテキスト入力のみを削除する必要があります。
これが私の質問です。ユーザーがボタンをクリックしたRemove
ときに、削除ボタンがクリックされたときにすべてのテキスト入力を削除するのではなく、削除されたファイル名に関連付けられたテキスト入力のみを削除するにはどうすればよいですか?
.hiddenimg
以下は、ファイル入力とそれが以下のhtmlテーブルに追加される方法、およびテキスト入力を格納する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);
}
ファイル入力フォームが追加され、テキスト入力が.hiddenimg
divタグに格納されるHTMLフォームとテーブル:
<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;
}