我正在通过href点击触发文件上传。
我正在尝试阻止除doc,docx和pdf之外的所有扩展名。
我没有得到正确的警报值。
<div class="cv"> Would you like to attach you CV? <a href="" id="resume_link">Click here</a></div>
<input type="file" id="resume" style="visibility: hidden">
Javascript:
var myfile="";
$('#resume_link').click(function() {
$('#resume').trigger('click');
myfile=$('#resume').val();
var ext = myfile.split('.').pop();
//var extension = myfile.substr( (myfile.lastIndexOf('.') +1) );
if(ext=="pdf" || ext=="docx" || ext=="doc"){
alert(ext);
}
else{
alert(ext);
}
})
MyFiddle..its显示错误
发布于 2013-08-01 19:20:48
最好在输入字段上使用change
事件。
更新源:
var myfile="";
$('#resume_link').click(function( e ) {
e.preventDefault();
$('#resume').trigger('click');
});
$('#resume').on( 'change', function() {
myfile= $( this ).val();
var ext = myfile.split('.').pop();
if(ext=="pdf" || ext=="docx" || ext=="doc"){
alert(ext);
} else{
alert(ext);
}
});
已更新jsFiddle。
发布于 2019-02-06 08:09:15
对于浏览器窗口中只有扩展名为doc和docx的acept文件,请尝试此选项
<input type="file" id="docpicker"
accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
发布于 2018-01-16 11:39:18
下面的代码对我很有效:
<input #fileInput type="file" id="avatar" accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
application/pdf means .pdf
application/msword means .doc
application/vnd.openxmlformats-officedocument.wordprocessingml.document means .docx
https://stackoverflow.com/questions/17992586
复制相似问题