Jquery cookie记住下拉菜单的最后状态 [英] Jquery cookie to remember last state of drop down menu

查看:1122
本文介绍了Jquery cookie记住下拉菜单的最后状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我这个,我找不到一个脚本在互联网上这样做在jQuery。我需要记住下拉菜单的最后状态,以便在页面加载下拉选项已经选择。 Cookie的过期日期应为90天或让我在脚本中指定。由于我已经在我的页面上使用jQuery,我想要这个cookie脚本使用jquery,jquery cookie插件。先谢谢你。我不是程序员,但我尝试。

Can anyone help me with this, I can't find a script on the internet that does this in jQuery. I need to remember the last state of drop down menu so that on page load the drop down option is already selected. The cookie expiration date should be 90 days or let me specify in the script. Since I already use jQuery on my page I want this cookie script to use jquery, jquery cookie plugin. Thank you in advance. I'm not a programmer, but I try.

<body onLoad="GETcookie()">
<select onChange="SETcookie()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>


推荐答案

您的代码看起来会像这样:

Your code would look a little something like this:

//checks if the cookie has been set

if($.cookie('remember_select') != null) {

    // set the option to selected that corresponds to what the cookie is set to

    $('.select_class option[value="' + $.cookie('remember_select') + '"]').attr('selected', 'selected');

}

// when a new option is selected this is triggered

$('.select_class').change(function() {

    // new cookie is set when the option is changed

    $.cookie('remember_select', $('.select_class option:selected').val(), { expires: 90, path: '/'});

});

这里是你选择

<select class="select_class">
    <option value="1">Row 1</option>
    <option value="2">Row 2</option>
    <option value="3">Row 3</option>
</select>

这里是一个关于jsFiddle的演示: http://jsfiddle.net/RastaLulz/3HxCF/3/

Here's a demo of it on jsFiddle: http://jsfiddle.net/RastaLulz/3HxCF/3/

这里是jQuery cookie插件, t已拥有: http://pastebin.com/CBueT8LP

Here's the jQuery cookie plugin incase you don't already have it: http://pastebin.com/CBueT8LP

这篇关于Jquery cookie记住下拉菜单的最后状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆