Jquery show/hide resetting when page reloads

后端 未结 6 1841
萌比男神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:54

    var setPaymentDivs = function() {
      var selected = $('input[name="paymentmethod"]:checked').val();
      $('div.payment').hide();
      $('#div' + selected).show();
    };
    
    $(function() {
      setPaymentDivs();
      $('input[name="paymentmethod"]').change(setPaymentDivs);
    });
    

    and add class="payment" to the divs. This abuses the fact that the back button and refresh remember the form values.

    Another way is to encode the current state somewhere - in a cookie, in a URL hash... and then extract it on load and set all the values accordingly. Advantage is that it works even if you shut down the browser, and in the case of URL hashes, even if you paste the URL to your friend's browser. (Well, maybe not for payment system :D but you know what I mean)

提交回复
热议问题