我设置了多个文件输入,我通过隐藏输入来设置样式,在 html/css 中创建自己的输入,然后通过 jQuery 控制原始输入以获得功能。
如果所有输入都已填充,我希望能够添加另一个输入和关联的 HTML,如下所示:
- 从 1 个输入开始
- 如果它被填充/使用,添加另一个输入
- 重复
- 删除已清除的输入,只留下 1 个空输入以供使用。
我尝试用我的代码创建一个 js fiddle,但它似乎无法通过它,所以这是我网站上的测试链接:LINK
和代码,JS:
$('.file-browse').click(function(){
var thisone = $(this).attr('id');
$('input[name="input-'+ thisone+'"]').click();
});
$('input[type="file"]').on('change', function(){
var thetext = $(this).val();
var thetextsplit = thetext.split('\\').pop();
var thisone = $(this).attr('name').split('-').pop();
if($('div[id^="info-file"]').text() == thetextsplit){
alert('you have already selected this file');
$(this).replaceWith( $(this).val('').clone( true ) );
}
else{
$('#info-'+ thisone).text(thetextsplit);
$('#clear-'+ thisone).fadeIn(100);
}
});
$('.file-clear').click(function(){
var thisone = $(this).attr('id').split('-').pop();
$('input[name="input'+ thisone +'"]').replaceWith( $('input[name="input'+ thisone +'"]').val('').clone( true ) );
$('#info-'+ thisone).text('');
$(this).fadeOut(100);
});
HTML:
<div class="file-container">
<div class="file-info" id="info-file1"></div>
<div class="file-browse" id="file1">Browse</div>
<div class="file-clear" id="clear-file1">X</div>
</div>
<div class="file-container">
<div class="file-info" id="info-file2"></div>
<div class="file-browse" id="file2">Browse</div>
<div class="file-clear" id="clear-file2">X</div>
</div>
<div class="file-container">
<div class="file-info" id="info-file3"></div>
<div class="file-browse" id="file3">Browse</div>
<div class="file-clear" id="clear-file3">X</div>
</div>
<form action="" method="POST" enctype="multipart/form-data">
<input type='file' name="input-file1" class="file-input-hidden" />
<input type='file' name="input-file2" class="file-input-hidden" />
<input type='file' name="input-file3" class="file-input-hidden" />
<input type="submit" style="clear:both; float:left;"/>
</form>
CSS:
.file-container{
clear:both;
float:left;
width:445px;
height:40px;
overflow:hidden;
padding:0;
margin:0;
}
.file-info{
float:left;
width:250px;
height:15px;
padding:5px;
border-radius:5px;
-webkit-border-radius:5px;
border:1px solid #95d2d2;
margin:0 20px 0 0;
font-family:Verdana, Geneva, sans-serif;
font-size:14px;
color:#373737;
overflow:hidden;
}
.file-browse{
float:left;
width:100px;
height:15px;
padding:3px 5px 8px 5px;
border-radius:5px;
-webkit-border-radius:5px;
border:1px solid #95d2d2;
background-color:#02b3b3;
color:#ffffff;
font-family:Arial, Gadget, sans-serif;
font-size:16px;
font-weight:bold;
letter-spacing:normal;
text-align:center;
cursor:pointer;
}
.file-input-hidden{
opacity:0;
visibility:hidden;
float:left;
}
.file-clear{
display:none;
float: left;
width: 18px;
height: 18px;
padding: 5px;
color: #ffffff;
background-color: #CC0000;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
font-weight: bold;
margin-left: 20px;
text-align: center;
border-radius: 5px;
cursor:pointer;
}
抱歉,这篇文章太长了!到目前为止,我不确定展示我的作品的最佳方式。