Why doesn't Firefox show the correct default select option?

前端 未结 11 1057
醉酒成梦
醉酒成梦 2020-11-28 07:02

I\'m making a web app to manage product SKUS. One part of that is to associate SKUs with product names. On each row of a table, I list a SKU and display a

相关标签:
11条回答
  • 2020-11-28 07:39

    Although this is old question, but below solution can help someone

    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.

    <form name="test_form" method="POST">
    <select name="city">
    <option value="1">Test</option>
    <option selected="selected" value="2">Test2</option>
    </selecct>
    

    Again remember :

    1. form must have the "name" attribute and
    2. "select" must be inside the form.
    0 讨论(0)
  • 2020-11-28 07:41

    Every time I ever had weird select option bugs in Firefox it was because I had multiple options marked as selected. Are you quite sure that only one is marked as such? Seems like you could get out of wack pretty easily if you are changing that with AJAX.

    0 讨论(0)
  • 2020-11-28 07:42

    FYI: in order to stop Firefox from restoring the previously selected option after page reload you can place the entire <form> containing the <select> options inside an <iframe>.

    When select boxes are in <iframe> and you reload the container page, Firefox finally behaves like ALL other browsers, by simply resetting select options.

    0 讨论(0)
  • 2020-11-28 07:43

    I've figured out. If you put onunload or $(window).unload (jquery) on your HTML with no-cache header, Firefox reloads the page and initialize DOM even from back button.

    0 讨论(0)
  • 2020-11-28 07:46

    Thanks to @BananaDeveloper (https://stackoverflow.com/a/8258154/2182349) - this is my solution to solve this problem on a single page in an application

    I didn't want to customize the off-the-shelf/open source application code

    <Files "page_that_does_not_work.php">
            SetOutputFilter INFLATE;SUBSTITUTE;DEFLATE
            Substitute 's/<select/<select autocomplete="off"/n'
            Substitute 's/<form/<form novalidate/n'
    </Files>
    

    Apache docs for mod_substitute https://httpd.apache.org/docs/2.4/mod/mod_substitute.html

    Thanks to: https://serverfault.com/questions/843905/apache-mod-substitute-works-in-curl-but-not-on-browser for the SetOutputFilter

    0 讨论(0)
  • 2020-11-28 07:48

    I had this same issue. I was trying to change the value of the select depending on which option had selected="selected", but Firefox wasn't working. It would always default to the first option.

    Chrome, Safari, etc worked when I did this:

    $( 'option[value="myVal"]' ).attr( 'selected', 'selected' );
    

    ... but this wasn't working in FF.

    So I tried:

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

    and it works.

    jQuery v1.9.1

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