Firefox ignores option selected=“selected”

前端 未结 20 1449
你的背包
你的背包 2020-11-28 21:59

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

相关标签:
20条回答
  • 2020-11-28 22:34

    In firefox, I've notice that the "selected" attribute will not work unless you place the select inside a form, where the form has a name attribute.

    0 讨论(0)
  • 2020-11-28 22:34

    With the name is better

    form id="UMForm" name="UMForm" class="form"
    

    The select will take the selected attribute

    0 讨论(0)
  • 2020-11-28 22:35

    You could call .reset() on the form before refreshing the page.

    0 讨论(0)
  • 2020-11-28 22:36

    This is my solution:

    var select = document.getElementById('my_select');
    for(var i=0; i < select.options.length; i++){
        select.options[i].selected = select.options[i].attributes.selected != undefined;
    }
    

    I just put that at the top of the page (with appropriate id set), and it works for me. Replacing the getElementById with a loop over all selects on the page, I leave as an exercise for the reader ;).

    0 讨论(0)
  • 2020-11-28 22:37

    Add autocomplete="off" HTML attribute to every select tag.

    (source: https://stackoverflow.com/a/8258154/260080)

    0 讨论(0)
  • 2020-11-28 22:38

    I'm using FF 25.0.1

    It ignore selected="" and selected="selected".

    But if I simply try selected the option is selected.

    Strange (non conformant) behaviour. I know selected is valid HTML5 and it's the shortest form, but I usually write code that also validates as wellformed XML, so that I can use any XML validation tool to check my results in a very strict way (and data exchange is very easy...)

    According to W3C, these variants should be valid on boolean attributes:

    HTML5:  boolAttr="" | boolAttr="boolAttr" | boolAttr
    XHTML5: boolAttr="" | boolAttr="boolAttr"
    

    I prefer the first one, as is it nearly as short as the last (not xml conformant) variant but should validate as both XHTML5 AND HTML5. So I hope, Mozilla will fix it!

    0 讨论(0)
提交回复
热议问题