首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jQuery在cookie中保存select值?

如何使用jQuery在cookie中保存select值?
EN

Stack Overflow用户
提问于 2015-07-03 17:44:46
回答 1查看 2.9K关注 0票数 1

首先,我使用jquery.cookie自举材料设计。我在从select保存cookie值方面有问题。

代码语言:javascript
复制
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);
代码语言:javascript
复制
<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中选择的用户。总是显示在曲奇第一位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-03 17:50:50

你为什么不试试:

代码语言:javascript
复制
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: '/'})
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31212030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档