Radio Boxes not resetting to “checked” status in Firefox

佐手、 提交于 2020-01-17 04:40:14

问题


I'm lost as to why my radio box form isn't working in Firefox 7.0.1, but works fine in IE, Chrome and Safari. The problem is that the radio boxes which are "checked" aren't resetting to what the HTML shows.

For example, if I change the temperature setting from "f" to "c" and and press Select, the action script sees the correct values. I then press Back in FF and refresh the window, "c" is still selected. As stated, this works as expected in other browsers, but not in FF.

I'm having a hard time believing that something this basic is broken in FF, but the HTML is straight forward.

-- Geoff

    <form name="form1" method="POST" action="weather_uom_set.php">        
    <h3>Precipitation</h3>
    <input type="radio" name="precip" value="in" checked="checked"/> Inches (")<br/>
    <input type="radio" name="precip" value="mm" /> Millimeters (mm)<br/>
    <h3>Temperature</h3>
    <input type="radio" name="temp" value="f" checked="checked"/> Fahrenheit (&deg;f)<br/>
    <input type="radio" name="temp" value="c" /> Celsius (&deg;c)<br/>
    <input type="submit" value="Submit" />
    </form>

回答1:


It may be because Firefox is remembering the previous form submission values, which can override any default choices you've defined in your HTML. Also, even though you haven't stated it here, I'd like to point out that Javascript can also override default HTML behavior as well, so if you do have a script running, make sure it also isn't setting/overriding a default value.




回答2:


Yes, this basic is broken in Firefox: https://stackoverflow.com/a/8779735/370290




回答3:


I originally made the mistake of setting checked=false on the "no" box instead of setting checked="true" on the "yes" box.

However, you might consider a similar fix I came up with to ensure that you get the value you want when the page loads:

<input type="radio" name="mybox" id="mybox_yes" value="1">
<input type="radio" name="mybox" id="mybox_no" checked="checked" value="0">

<script>

    var mybox = 0; // value 1 or 0 inserted with server side script

    if(mybox == 1){
        var fix_checkbox = function(){
            document.getElementById("mybox_yes").checked=true;
        };
    }
    else
    {
        var fix_checkbox = function(){
        document.getElementById("mybox_no").checked=true;
        };
    }

    setTimeout(fix_checkbox,1000);

</script>

It might be better to do the logic server side then only output the line you want:

<script>
    setTimeout(function(){document.getElementById("mybox_no").checked=true;},1000);
</script>


来源:https://stackoverflow.com/questions/7876384/radio-boxes-not-resetting-to-checked-status-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!