2012-09-23 63 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

相關問題