您的位置:首页 > Web前端 > JavaScript

关于onchange提交一次file文件的问题

2017-08-09 16:31 459 查看
1,  思想:  在没有刷新页面的时候,  默认的  file文件已经存在,  当改变的  还是原来的file文件,这个时候  onchange不在刷新

网上的做法:

1,用jQuery的live代替直接使用change

    <input
name="fileToUpload" type="file" id="inputFileID"  />

    $("#UploadFile").live('change',
function(){  });

2:onchange后,生成一个新input type file代替旧的。       替换的原则

$('#inputFileID').replaceWith('<input name="fileToUpload" type="file" id="inputFileID"  />');

我的想法:

在onchange后重置input中的value值,我把他设置为空:

var input = document.getElementById('fileinput');
input.value='';


我的实例:

view

<input type="file" name="excel"  style="position: absolute;width: 90px;height: 40px;opacity: 0;
-ms-filter: 'alpha(opacity=0)';cursor:pointer;" value="" id="fileinput" onchange="fileupload()" >


js

function fileupload(){
$('#form').ajaxSubmit({
url:'',
dataType:'json',
type:'post',
data:$('#form').serialize(),
success:function(res){
var input = document.getElementById('fileinput'); input.value='';
if (res.status == 0) {
layer.msg(res.msg);
} else {
layer.msg(res.msg, {icon: 1}, function () {
location.href = '';
});
}

},
error:function(xhr, type){
layer.msg('');
var input = document.getElementById('fileinput'); input.value='';
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js
相关文章推荐