1

这是来自的 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/

​</p>

4

2 回答 2

1

文件名可以未定义您应该尝试:

if (filename != undefined && filename.length > 0)

你忘了 #

$('#submit').click(validate_ext);

你为什么写

if (!(ext == "xml"))

代替

if (ext.toUpperCase() != "XML")

!= 比 !(==) 简单,并且您的文件扩展名可以包含大写字符

于 2012-04-04T12:41:28.613 回答
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;
    }

这是工作代码http://jsfiddle.net/vikastyagi87/XugLp/29/

于 2012-04-04T12:32:41.970 回答