jQuery:获取从 <input type ="file"/> 中选择的文件名

标签 jquery events copy file-io

此代码应该在 IE 中 可以工作(甚至不要在 Firefox 中测试它),但事实并非如此。 我想要的是显示附件的名称。有帮助吗?

<html>
<head>
  <title>example</title>    
  <script type="text/javascript" src="../js/jquery.js"></script>
  <script type="text/javascript">  
        $(document).ready( function(){            
      $("#attach").after("<input id='fakeAttach' type='button' value='attach a file' />");      
      $("#fakeAttach").click(function() {            
        $("#attach").click();        
        $("#maxSize").after("<div id='temporary'><span id='attachedFile'></span><input id='remove' type='button' value='remove' /></div>");        
        $('#attach').change(function(){
          $("#fakeAttach").attr("disabled","disabled");          
          $("#attachedFile").html($(this).val());
        });        
        $("#remove").click(function(e){
          e.preventDefault();
          $("#attach").replaceWith($("#attach").clone());
          $("#fakeAttach").attr("disabled","");
          $("#temporary").remove();
        });
      })
    }); 
  </script> 
</head>
<body>
  <input id="attach" type="file" /><span id="maxSize">(less than 1MB)</span>    
</body>
</html>

最佳答案

写起来就这么简单:

$('input[type=file]').val()

无论如何,我建议使用名称或 ID 属性来选择您的输入。 对于事件,它应该是这样的:

$('input[type=file]').change(function(e){
  $in=$(this);
  $in.next().html($in.val());
});

关于jQuery:获取从 &lt;input type ="file"/> 中选择的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1299052/

相关文章:

jquery - 单击事件时未获取 jquery 中文本框的值

node.js - Angular2点击事件不起作用

PostgreSQL - 复制自 - "unacceptable encoding"错误

jquery - JCarousel Lite(多个轮播问题)

javascript - 如何将 Jquery 滚动事件绑定(bind)到动态添加的元素?

javascript - 全选/取消全选 - 复选框和表格数据

php - 使用 microsoft graph API 创建事件时设置自己的事件 ID

C#:需要我的一个类来触发另一个类中的事件来更新文本框

javascript - 将文本区域文本复制到剪贴板 html/javascript

javascript - 通过单击将 css 类附加到多个 div