【问题标题】:How to save value from select in cookie using jQuery?如何使用jQuery从cookie中的选择中保存值?
【发布时间】:2015-07-03 17:44:46
【问题描述】:

首先,我使用的是jquery.cookieBootstrap material design。我从选择中保存 cookie 值时遇到问题。

var getval = $.cookie('selected-val');
if (getval == 10000) {
  $("#refresh option[value='10000']").attr("selected", "selected");
};
if (getval == 20000) {
  $("#refresh option[value='20000']").attr("selected", "selected");
};
if (getval == 30000) {
  $("#refresh option[value='30000']").attr("selected", "selected");
};
if (getval == 60000) {
  $("#refresh option[value='60000']").attr("selected", "selected");
};
if (getval == 180000) {
  $("#refresh option[value='180000']").attr("selected", "selected");
};
var valsel = $("#refresh").val();
$.cookie('selected-val', '' + valsel + '', {
  expires: 365,
  path: '/'
});

setInterval(function() {
  myfunction()
}, getval);
<div class="form-group">
  <div class="col-lg-12">
    <select class="form-control" id="refresh">
      <option id="ref-1" value="10000">10 s</option>
      <option id="ref-2" value="20000">20 s</option>
      <option id="ref-3" value="30000">30 s</option>
      <option id="ref-4" value="60000">1 min</option>
      <option id="ref-5" value="180000">3 min</option>
    </select>
    <br>
  </div>
</div>

我希望用户可以在我的函数中选择刷新时间,但我不知道如何保存用户在 cookie 中选择的内容。始终显示在 cookie 的首位。

【问题讨论】:

    标签: javascript jquery cookies bootstrap-material-design


    【解决方案1】:

    你为什么不试试:

    jQuery(document).ready(function(){
      var selectedVal = jQuery.cookie("selected-val");
      if (selectedVal) {
        jQuery("#refresh").val(selectedVal);
      }
      jQuery("#refresh").on("change", function(){
        var selection = jQuery(this).val();
        jQuery.cookie("selected-val", selection, {expires: 365, path: '/'})
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多