Firefox ignores option selected=“selected”

2020-01-27 11:17发布

If you change a dropdown and refresh the page, Firefox seems to ignore the selected attribute.

<option selected="selected" value="Test">Test</option>

It will in fact select the option you had previously selected (before the refresh). This ends up being a problem for me since there is an event triggered on the dropdown which changes other things. Is there a way to make firefox stop this behavior (other than firing another event when the page loads)?

20条回答
Deceive 欺骗
2楼-- · 2020-01-27 12:15

Neither autocomplete="off" or placing it inside a form works for me.

What worked was to only use the attribute selected with no "value" like this:

<option @(Model.Source == TermSource.Instagram ? "selected" : "")>
    Instagram
</option>
<option @(Model.Source == TermSource.Facebook ? "selected" : "")>
    Facebook
</option>

so either it renders <option selected>...</option>, or just <option>...</option>

查看更多
你好瞎i
3楼-- · 2020-01-27 12:19
<option selected="selected" value="Test">Test</option>

In this case this worked both for Chrome and Firefox.

$('option[value="Test"]').prop('selected', true);

I was using .attr() instead of .prop()

查看更多
登录 后发表回答