我想提醒一下"请输入文件.docx和.pdf"所以我需要文件的扩展名......
{{1}}
答案 0 :(得分:1)
使用分割功能
$("#file").val('file.ext');
$("#upload").click(function () {
var filename = $("#file").val();
var aux = filename.split('.');
var extension = aux[aux.length -1].toUpperCase();
if(extension === 'DOC'
|| extension === 'DOCX'
|| extension === 'PDF'
){
alert('Valid extension.');
}else{
alert('Invalid extension: ' + extension + '. Only: DOC, DOCX, PDF are allowed.');
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='file'/>
<button id='upload'>Upload</button>
&#13;
答案 1 :(得分:0)
这里有一个有效的例子。只需在提供的路径上使用.split()
$(".upload").click(function () {
var filename = $("#file").val();
var pathParts = filename.split('/'); // Split the path on '/': ['path', 'to', 'file.ext']
var filename = pathParts.pop(); // Take the last element. This is a file name: 'file.ext'
var filenameParts filename.split('.'); // Split the file name on the '.': ['file', 'ext']
var extension = filenameParts[1]; // Take the extension: 'ext'
});
或者你可以在一行中完成:
var extension = filename.split('/').pop().split('.')[1];