【问题标题】:InvalidStateError: Failed to set the 'value' property on 'HTMLInputElementInvalidStateError:无法在“HTMLInputElement”上设置“值”属性
【发布时间】:2017-04-25 13:57:33
【问题描述】:

我有一个类型为文件的输入标签

<input type="file" id="fileToUpload" name="file" tabindex="6"  value=""/>

当我选择文件时,它给了我一个错误。

jquery-2.1.3.js:7350 Uncaught InvalidStateError: 无法设置 'HTMLInputElement' 上的 'value' 属性:此输入元素接受 文件名,只能以编程方式设置为空字符串。

查看了jQuery代码,发现是因为

 hooks = jQuery.valHooks[this.type] || jQuery.valHooks[this.nodeName.toLowerCase()];

                // If set returns undefined, fall back to normal setting
                if (!hooks || !("set" in hooks) || hooks.set(this, val, "value") === undefined) {
                    this.value = val;
                }

来自 jQuery 文件的代码 sn-p。

jQuery.valHooks 对象仅适用于复选框、收音机、选项和 选择。文件没有对象。

【问题讨论】:

  • 你想在这里做点什么吗?你能给我们你的javascript代码吗?
  • 我正在尝试对 $("#fileToUpload").change(function () { if ($('#fileToUpload').val() != "") { / /一些操作 } });但由于更改错误没有被触发。
  • 所以当你点击“选择文件”时会弹出错误?
  • 错误没有弹出,但由于控制台中的错误,我的代码在 .更改事件未执行。
  • 所以当你加载页面时,错误已经在控制台中了?如果删除函数,错误仍然存​​在怎么办?如果是这样,我建议你改变你的 jQuery 版本。

标签: jquery


【解决方案1】:

老实说,这甚至不是一个好的答案,但我尝试了一个快速修复,它奏效了。

//Just to remove error inside the console.
//If error exists then onChange event is not fired.
var fileHook = {
    get: function (elem) {
        return this.value;
    },
    set: function (elem, value) {
        return this.value = value;
    }
}
$.valHooks["file"] = fileHook;

如果有的话,欢迎解释利弊。

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2016-03-06
    • 2014-12-31
    相关资源
    最近更新 更多