IE 9 jQuery not setting input value

2019-02-20 08:12发布

问题:

my question is simple:

I have an input file field, and i want to limit it to only accept .GIFs via Jquery , and if the format is wrong, set the input value blank.

The problem is that on IE9, the .val('') does not work. Any ideas?

My jQuery:

$('input[type=file]').change(function() {
    var val = $(this).val();
    switch (val.substring(val.lastIndexOf('.') + 1).toLowerCase()) {
    case 'gif':
        break;
    default:
        // error message here
        alert("Wrong Format");
        $(this).val('');
        break;
    }
});​

回答1:

From IE8 it is readonly try:

$("input[type='file']").replaceWith($("input[type='file']").clone(true));

Picked from here : SO Anwser