Javascript 使用$(this.val(";)重置输入文件不使用IE8

Javascript 使用$(this.val(";)重置输入文件不使用IE8,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,可能重复: 如果输入字段符合某些条件,我想使用$(this).val(“”)重置输入字段: 在Firefox中,该字段设置为”,但在IE中,该字段不会按预期更改。我使用的是正确的函数.val(“”)?请参阅: 因此,在你的情况下: $(this).replaceWith($(this).clone(true)); 而不是$(this).val(“”)行 更新: 为了利用允许您修改input:file元素的浏览器,我将使用如下内容: $(".checkimgextension").on("ch

可能重复:

如果输入字段符合某些条件,我想使用
$(this).val(“”)重置输入字段

在Firefox中,该字段设置为
,但在IE中,该字段不会按预期更改。我使用的是正确的函数
.val(“”)

请参阅:

因此,在你的情况下:

$(this).replaceWith($(this).clone(true));
而不是
$(this).val(“”)

更新:

为了利用允许您修改
input:file
元素的浏览器,我将使用如下内容:

$(".checkimgextension").on("change", function () {
    var $this = $(this);

    if (some conditions) {
        alert("wrong:...");
        $this.val("");
        var new_val = $this.val();
        if (new_val !== "") {
            $this.replaceWith($this.clone(true));
        }
    }
});

通过这种方式,它试图首先将值设置为空,如果不成功,请使用
replaceWith
方法。

在IE:中对我很好,否则就不会发生。在你的标题中,你说“file”——你的意思是说
input type=“file”
?伙计们,很可能是“file”输入,您不能在IE中通过JavaScript设置其值。如果可能,请更新以避免
replacetwith
$(this).replaceWith($(this).clone(true));
$(".checkimgextension").on("change", function () {
    var $this = $(this);

    if (some conditions) {
        alert("wrong:...");
        $this.val("");
        var new_val = $this.val();
        if (new_val !== "") {
            $this.replaceWith($this.clone(true));
        }
    }
});