我有一个表单,我动态添加上传文件与附加功能,但我也希望能够删除未使用的字段.这是html标记
<span class="inputname">Project Images:
<a href="#" class="add_project_file"><img src="images/add_small.gif" border="0"></a>
</span>
<span class="project_images">
<input name="upload_project_images[]" type="file" /><br/>
</span>
Run Code Online (Sandbox Code Playgroud)
现在,如果他们点击"添加"gif,则会在此jquery中添加一个新行
$('a.add_project_file').click(function() {
$(".project_images").append('<input name="upload_project_images[]" type="file" class="new_project_image" /> <a href="#" class="remove_project_file" border="2"><img src="images/delete.gif"></a><br/>');
return false;
});
Run Code Online (Sandbox Code Playgroud)
要删除输入框,我尝试添加类"remove_project_file",然后添加此功能.
$('a.remove_project_file').click(function() {
$('.project_images').remove();
return false;
});
Run Code Online (Sandbox Code Playgroud)
我认为应该有一个更简单的方法来做到这一点.也许我需要使用$(this)函数进行删除.另一种可能的解决方案是扩展"添加项目文件"以同时添加和删除字段.
你们中的任何一个JQuery向导都有任何想法会很棒
Ale*_*ett 49
由于这是一个开放式的问题,我只想告诉你如何自己实现这样的事情.
<span class="inputname">
Project Images:
<a href="#" class="add_project_file">
<img src="images/add_small.gif" border="0" />
</a>
</span>
<ul class="project_images">
<li><input name="upload_project_images[]" type="file" /></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
将文件输入包含在li元素内部允许在单击时轻松删除"删除"链接的父级.这样做的jQuery已经接近你已经拥有的:
// Add new input with associated 'remove' link when 'add' button is clicked.
$('.add_project_file').click(function(e) {
e.preventDefault();
$(".project_images").append(
'<li>'
+ '<input name="upload_project_images[]" type="file" class="new_project_image" /> '
+ '<a href="#" class="remove_project_file" border="2"><img src="images/delete.gif" /></a>'
+ '</li>');
});
// Remove parent of 'remove' link when link is clicked.
$('.project_images').on('click', '.remove_project_file', function(e) {
e.preventDefault();
$(this).parent().remove();
});
Run Code Online (Sandbox Code Playgroud)
您可以在附加之前调用重置功能。像这样:
function resetNewReviewBoardForm() {
$("#Description").val('');
$("#PersonName").text('');
$("#members").empty(); //this one what worked in my case
$("#EmailNotification").val('False');
}
Run Code Online (Sandbox Code Playgroud)