0
我有一个多文件输入的设置,我通过隐藏输入,在html/css中创建我自己的样式,然后通过jQuery控制原始输入获得功能。动态添加或删除输入字段,因为它们是必需的
我希望能够再添加一个输入和相关的HTML,如果所有的投入都充满,就像这样:
- 从1开始输入
- 如果这被涂抹/使用,添加其他输入
- 重复
- 删除清除的输入,只留下1个空的输入使用。
我尝试创建我的代码一个js小提琴,但它似乎并没有通过它来工作,所以下面就以我的网站上测试的链接: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;
}
对不起长篇帖子!我不确定迄今为止展示我的作品的最佳方式。
这个作品非常好!谢谢。唯一不是它检查文件是否被选中的地方:if($ visible.find(“。file-info”)。text()== val){ alert('You have已经选择了这个文件'); $(this).replaceWith($(this).val('')。clone(true)); }但在我的脑海中读它似乎应该?有任何想法吗? – rpsep2 2013-02-10 19:19:26
啊,是的,测试是不正确的。新的'val'需要针对* each *现有的'.file-info' div文本进行测试。你的原始方法,如果它的工作,可能是最好的,但我只是用不同的东西编辑我的答案;即使用'.map()'循环现有的'.file-info' divs查找匹配,然后测试生成的jQuery obj的长度。 – 2013-02-10 22:26:59
刚刚测试过,它是'.filter()',而不是'.map()'。 – 2013-02-11 05:12:08