·工作中遇到了一个有趣的问题:
<form>
<em>+</em>
<input type=file ></input>
<./form>
css中使得input框覆盖了+号; 当点击+号时平常的弹出选择文件,选到文件了现实文件名,+号变成-号
再按inpu框时 清楚已经选择的 文件
$(document).ready(function() {
var fileType = $('input[name="fileType"]').val();
var title=fileType == 'img' ? "插入图片":"添加附件";
inputChange(title);
function inputChange(title){
$('input[type="file"]').change(function() {
var ff = $(this).parent().parent();
var fileName = $(this).val();
if(fileName.indexOf("\\") > -1) {
var startIndex = fileName.lastIndexOf("\\");
fileName = fileName.substring(startIndex + 1, fileName.length);
}
ff.attr("class","btn_delPic");
ff.children(':first').html("<em>-</em>" + fileName);
$(this).html(fileName);
inputClick($(this), title);
});
}
function inputClick(obj, title){
obj.click(function(){
var name = obj.attr("name");
obj.parent().parent().children(':first').html("<em>+</em>" + title);
obj.parent().parent().attr("class","btn_addPic");
obj.parent().html('<input class="filePrew" name='+name+' type="file"/>');
inputChange(title);
return false;
});
}
});
版权声明:本文为mingluoxuan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。