这是来自
的html <form action="http://localhost/own/import.php" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="xmlfile" id="file1">
<input type="file" name="uploaded_image" id="file2">
<input type="submit" id="submit" name="Submit" value="Submit" style="margin:0 0 0 150px;">
这是java脚本文件
function getExt(filename) {
var dot_pos = filename.lastIndexOf(".");
if (dot_pos == -1) {
return "";
}
return filename.substr(dot_pos + 1).toLowerCase();
}
function validate_ext() {
var filename = document.getElementById("file1").value;
var filename2 = document.getElementById("file2").value;
var ext = getExt(filename);
var ext2 = getExt(filename2);
if (filename != "") {
if (!(ext == "xml")) {
alert("Please upload XML file format only");
return false;
}
}
if (filename2 != "") {
if (!((ext2 == "jpg") || (ext2 == "jpeg") || (ext2 == "png"))) {
alert("Please upload jpg, jpeg, png file format only");
return false;
}
}
return true;
}
$(document).ready(function() {
$('#submit').click(validate_ext);
alert("test");
});
我也在这里分享代码http://jsfiddle.net/vikastyagi87/XugLp/18/
答案 0 :(得分:1)
文件名可以是未定义的 你应该试试:
if (filename != undefined && filename.length > 0)
你忘了#
$('#submit').click(validate_ext);
你为什么写
if (!(ext == "xml"))
而不是
if (ext.toUpperCase() != "XML")
!=比简单!(==) 和 您的文件扩展名可以包含大写字符
答案 1 :(得分:0)
我错误地跟着
行了if (filename != "") {
if (!(ext == "xml")) {
alert("Please upload XML file format only");
return false;
}
}
if (filename2 != "") {
if (!((ext2 == "jpg") || (ext2 == "jpeg") || (ext2 == "png"))) {
alert("Please upload jpg, jpeg, png file format only");
return false;
}
}
但现在改变后它正在运作
if (!(ext == "xml")) {
alert("Please upload XML file format only");
return false;
}
if (!((ext2 == "jpg") || (ext2 == "jpeg") || (ext2 == "png"))) {
alert("Please upload jpg, jpeg, png file format only");
return false;
}