【问题标题】:<input type="file"> is undefined after simple file select<input type="file"> 简单文件选择后未定义
【发布时间】:2015-06-21 20:20:51
【问题描述】:

我有:

<input type="file" name="fileToUpload" id="fileToUpload" />

单击“选择文件”按钮允许我使用 Finder/Explorer/whatever 选择文件。当我点击“打开”并返回到我的表单时,文本“未选择文件”仍然存在。

如果我使用以下方法检查值:

$("#fileToUpload").change(function() {
        console.log($("#fileToUpload").value);
    });

值未定义。我正在撕扯我的头发,因为这太简单了,而且它不起作用。我会错过什么?

【问题讨论】:

  • 这又抬头了。代码在:vzaarsupport.com/alasdair/demos/api_uploader_H5_dev 如果您单击“选择文件”按钮并选择一个文件,即使在选择文件后,文本“未选择文件”仍然存在。这么简单的事情怎么会这么难? :)

标签: html forms file input undefined


【解决方案1】:

jQuery 对象没有.value 属性。他们有一个.val() 方法。

所以你想要

console.log($("#fileToUpload").val());

【讨论】:

  • 不,他们有一个val() 方法。
  • 道歉。在 JS 和 jQuery 之间切换...我将 jQuery 更改为 console.log($("#fileToUpload").val());,现在可以正确报告文件名,但 &lt;input&gt; 仍然显示“未选择文件”
  • 我尝试从$("#fileToUpload").val() 显式设置fileToUpload 的值,但出现错误:Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.。但是,文件选择器现在显示选定的文件名!
猜你喜欢
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
相关资源
最近更新 更多