2012-09-23 62 views
2

长话短说,我需要能够阻止input type="file"的默认操作。换句话说,当用户点击“浏览”或“选择文件”时,我不想显示系统的打开对话框。我已经有了替换对话框,但系统的打开对话框仍然出现。防止html输入类型文件显示打开对话框

下面是我正在尝试完成此操作的示例。 (PS:我正在使用Chrome 21)

<html> 
<head> 

<script type="text/javascript"> 
<!-- 

file_onclick = function() 
{ 
    // Show custom dialog instead... 
    event.stopPropagation(); // Doesn't work 
    return false; // Neither does this 
}; 

//--> 
</script> 

</head> 
<body> 
    <input type="file" onclick="javascript: file_onclick();" /> 
</body> 
</html> 

任何想法?

回答

0

明白了。我需要禁用该标记,然后使用setTimeout方法重新启用它。

<html> 
<head> 

<script type="text/javascript"> 
<!-- 

file_onclick = function(o) 
{ 
    // Show custom dialog instead... 

    o.disabled = true; 
    setTimeout(function() { o.disabled = false; }, 1); 
}; 

//--> 
</script> 

</head> 
<body> 
    <input type="file" onclick="javascript: file_onclick(this);" /> 
</body> 
</html> 
+0

这不适用于IE9。 – heinob

+0

只在Chrome 20中测试过 – vbguyny

+0

请在此检查正确答案:http://stackoverflow.com/questions/7350134/is-it-possible-to-prevent-file-dialog-from-appearing-why – jayarjo

2

如何

<input type="file" onclick="return false" /> 

,或者如果你需要的file_onclick功能

<html> 
<head> 

<script type="text/javascript"> 
<!-- 

file_onclick = function() 
{ 
    // Show custom dialog instead... 
    return false; 
}; 

//--> 
</script> 

</head> 
<body> 
    <input type="file" onclick="return file_onclick();" /> 
</body> 
</html> 
+0

谢谢,但这是我第一次尝试,它不起作用。 – vbguyny

相关问题