简体   繁体   中英

Is it possible to retain form radio selections using the back button?

The user can select a radio button and depending on which radio button is selected, an input field can show asking for additional information. The user submits the form; then say they need to go back and make a change to the form. When they go back, the input field is no longer showing but the correct radio button is selected.

The answer only needs to work in IE8+. Using jQuery as part of the solution is also an option.

--==UPDATED 7/31/2012 using the jQuery plugin simsalabim ==--

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

<!---<script src="sisyphus.min.js"></script>--->
<script type="text/javascript" src="https://raw.github.com/simsalabim/sisyphus/master/sisyphus.min.js"></script>
<script>
$(document).ready(function(){
    $('#form1').sisyphus();
});
</script>
</head>

<body>
<script>
    function ToggleColorOptions(el) {
    if (el == 'Std')
            {
        $('#Option1').show();
        $('#Option2').hide();
    }
        else if (el == 'Non')
            {
                $('#Option1').hide();
                $('#Option2').show();
            }
        }
</script>

<form id="form1" action="myTesta.cfm" method="post">
<input name="StdRNonStdOption" type="radio" id="StdRNonStdOption_std" onclick="ToggleColorOptions('Std');" value="Std" />
Standard <br />
<input name="StdRNonStdOption" type="radio" id="StdRNonStdOption_non" onclick="ToggleColorOptions('Non');" value="Non" />
Non-Standard <br />

<div id="Option1" style="display:none;">
    <select name="ThisColor" >
        <option></option>
        <option>Blue</option>
        <option>Green</option>
    </select>
</div>
<div id="Option2" style="display:none;">
    <input name="ThisColor" type="text" size="20" maxlength="51" />
</div>
<input type="submit" value="Save" />

Use something like Sisyphus, gmail-like client side localStorage plugin. Probably your best option...

http://simsalabim.github.com/sisyphus/

Your solution in JQuery:

Make an input type button

Put your expanded onclick options in a div with a class name someexpandedoptions

On the click event toggle visibility

$('.someexpandedoptions').css('display','inline');

$('.someexpandedoptions').css('display','none');

It's as easy as that.

Just make sure to properly initialize your buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM