0

我在我的网页中添加了一个表单,允许上传多个文件。我添加了输入<input type="file" name="photo_upload[]" id="photo_upload" multiple="multiple" />

我尝试添加一个警告,该警告仅在浏览器不支持多文件功能时才可见。我试过这段代码:

<script type="text/javascript">
var testrange=document.createElement("input")
testrange.setAttribute("multiple", "multiple")
if (testrange.multiple=="multiple"){
    document.getElementById('multiple_warning').style.display = 'none';
}
</script>
<div id="multiple_warning" style="background:#FFFF99; border:dashed; border-width:thin;">Sorry, but your browser does not support the multiple file upload feature.<br />
If you have more than one photo to send us, please send it after you recieved your comformation email.</div>

但无论如何它总是显示“multiple_warning”。我怎样才能让它正常运行?

4

3 回答 3

1

您可以使用 Modernizr 来测试特定属性。它是一个开源的跨浏览器 html 5 检测库

if(Modernizr.input[attribute])){
    alert("Attribute exists");
}
else{
    //error handling
}

见这里http://modernizr.com/docs/#input

于 2012-07-10T06:05:30.177 回答
0

检查控制台,看看为什么你的测试总是失败;)

testrange.setAttribute("multiple", "multiple")
console.log(testrange.multiple);

您可以使用此代码段检查此功能

if (!("multiple" in testrange)) {
    alert("no multiple upload supported");
}
于 2012-07-10T06:02:32.790 回答
0

使用它而不是您拥有的 JavaScript:

var testrange = document.createElement("input")
if ("multiple" in testrange) {
    document.getElementById("multiple_warning").style.display = "none";
}

如果您要进行大量特征检测,请考虑使用Modernizr来测试这些东西,而不是创建自己的测试。它解决了很多极端情况,并且会为您省去很多麻烦。

于 2012-07-10T06:07:23.100 回答