【问题标题】:Prevent html input type file from showing open dialog防止 html 输入类型文件显示打开的对话框
【发布时间】:2012-09-23 18:33:33
【问题描述】:

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

下面是我目前正在尝试实现的示例。 (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>

有什么想法吗?

【问题讨论】:

    标签: javascript html input-type-file


    【解决方案1】:

    怎么样

    <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>
    

    【讨论】:

    • 谢谢,但这是我尝试的第一件事,但它不起作用。
    【解决方案2】:

    知道了。我需要禁用标签,然后使用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>
    

    【讨论】:

    猜你喜欢
    • 2019-06-21
    • 2010-11-08
    • 2017-11-27
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    相关资源
    最近更新 更多