Setting jquery cookies for buttonset

久未见 提交于 2019-12-12 02:34:34

问题


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
  });
});

回答1:


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});
        });
    });
});


来源:https://stackoverflow.com/questions/20754634/setting-jquery-cookies-for-buttonset

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