简体   繁体   English

cookie记住下拉选择

[英]cookie to remember dropdown selection

I have a simple dropdown and user selects a choice and the page refreshes with the selection added to the URL as a querystring. 我有一个简单的下拉菜单,用户选择了一个选项,页面刷新,并且该选择作为查询字符串添加到URL中。 But i want to also keep the selected state of dropdown after the refresh. 但我也想在刷新后保持下拉菜单的选定状态。 How do i do that using jquery or cookie? 我如何使用jquery或cookie?

<select id="MyDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')">

  <option value="http://mysite.com/default1.aspx?alpha=A">A</option>
  <option value="http://mysite.com/default1.aspx?alpha=B">B</option>
  <option value="http://mysite.com/default1.aspx?alpha=C">C</option>
</select>

You can use the jquery cookie plugin and write code as shown below 您可以使用jquery cookie插件并编写代码,如下所示

$('#MyDropDown').change(function() {
    $.cookie('mycookie', $(this).val(), {
             expires: 365}
             );
}

A better way to save the state without putting data on each html request is to use HTML5 Local storage. 保存状态而不在每个html请求上添加数据的更好方法是使用HTML5本地存储。 Here is a good example how to use it: http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-learning-about-html5-local-storage/ 这是一个如何使用它的好例子: http : //net.tutsplus.com/tutorials/html-css-techniques/quick-tip-learning-about-html5-local-storage/

Does setting a cookie not work? 设置cookie无效吗?

<select id="MyDropDown" onchange="document.cookie=this.selectedIndex; window.open(this.options[this.selectedIndex].value,'_top')">

You could also just extract the value of "alpha" passed with the URL. 您也可以提取URL传递的“ alpha”值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM