Jquery show/hide resetting when page reloads

后端 未结 6 1840
萌比男神i
萌比男神i 2021-01-27 05:16

I\'m new to jquery, but I\'m trying to use it to create a multi-step tabbed form.

One one of the pages, I have radio buttons that will show several fields depending on t

6条回答
  •  走了就别回头了
    2021-01-27 05:49

    Cookies are your friends.

    http://www.quirksmode.org/js/cookies.html

    --or, with a jQuery plugin--

    http://plugins.jquery.com/cookie

    Or, localStorage is your friend!

    $(document).ready(function() {
    
    //...stuff
    if (localStorage['payment']) {
        if (localStorage['payment'] === 'credit')
            $("#divcheque").hide();
        else
            $("#divcredit").hide();
    }
    else {
        $("#divcheque").hide();
        $("#divcredit").hide();
    }    
    
    $("input[name='paymentmethod']").change(function() {
        if( $("input[name='paymentmethod']:checked").val() == "cheque")
        {
            $("#divcredit").hide();
            $("#divcheque").show();
            localStorage['payment'] = 'cheque';
        }
        else if($("input[name='paymentmethod']:checked").val()== "creditcard")
        {
            $("#divcredit").show();
            $("#divcheque").hide();
            localStorage['payment'] = 'credit';
        }
    });
    

    http://diveintohtml5.ep.io/storage.html

    For cross-browser compatibility, use cookies. But in both ways, it would work even after your users have left for some time; something you might or might not want.

提交回复
热议问题