Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

Setting jquery cookies for buttonset

I'm trying to setup jquery cookies but I'm not able to get it to work. here's the code i'm using...

<div id="tabs-5">
 <form>
  <fieldset id="units" name="units">
   <legend>Units</legend>
    <center>
     <div id="radio">
      <label title="Select units of measurement to work with, Inches or Millimeters"></label>
      <label for="inches">Inches</label>
      <input type="radio" id="inches" name="unit" value="inches" onclick='valInches();' />
      <label for="mm">Millimeters</label>
      <input type="radio" id="mm" name="unit" value="mm" onclick='valMm();' />
     </div>
    </center>
  </fieldset> <!--end "units"-->
 </form>
</div> <!-- end tabs-5-->

$(function() {
 var selected = $.cookie('radio'); // Retrieve cookie value
  $('input[name="unit"][value="' + selected + '"]').attr('checked', true); //Check matching button
  $('input[name="unit"]').click(function() {
   $.cookie('radio', $(this).val(), {expires: 365}); // Save cookie
  });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found what the problem was... the code I have works fine but the problem was the buttons weren't getting 'clicked'.

So I added this to refresh the button 'click' and now everything works fine.

$('input[value="' + units + '"]').prop('checked', true).button('refresh');

EDIT: Setting the buttonset class to class=radioButtons this code does what I need for any buttonset...

$(function(){                                                                               
    var radioButtonSet=$('.setCookies').find('.radioButtons');
    radioButtonSet.buttonset();
    var radio=$('.radioButtons').find(':radio');

    radio.each(function(){
        var selected=$.cookie(this.name);
        $('#'+selected).prop('checked', true).button('refresh');

        $(this).change(function() {
            $.cookie(this.name, $(this).val(), {expires: 365});
        });
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...