防止选择选项更改回默认值 [英] Prevent select option from changing back to default

查看:90
本文介绍了防止选择选项更改回默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,该页面的下拉菜单中有6个选项.我使用以下代码将默认选择选项全名"设置为

I have a page that has 6 options in a drop down menu. I use the below code to make the default select option "Full Name"

       $(function(){
         $('select option[value="Full Name"]').attr("selected",true);
       });      

这很好用,但是由于页面本身被调用,并且例如,我更改了搜索团队的选项,所以当页面加载时,默认选项显然会变回全名.我需要将其更改回先前选择的位置.

this works fine however since the page is called on itself and I change the option to search for Team for example, when the page loads the default option will obviously change back to Full Name. I need to change it back to what was previously selected.

这听起来很容易,我现在想出一个空白.

This sounds easy to do and I'm just coming up with a blank at the moment.

感谢您的帮助.

推荐答案

您将需要使用服务器或客户端技术来存储下拉菜单的状态.

You will need to store the state of the dropdown either using server or client side technology.

在客户端,您可以使用Cookie或html5存储,例如本地存储存储选定的值,并且当重新访问该页面并且有一个存储的值时,您可以选择该值而不是默认值

In client side you can use a cookie or html5 storage like local storage to store the selected value and when the page is revisited and there is a stored value then you can select that value instead of the default value

如果您打算使用cookie来存储信息,那么您可以想到一个 this之类的jQuery插件.

If you are planning to use cookie to store the information, then you can think of a jQuery plugin like this or this

抽象实现可能看起来像

$('select').change(function(){
    storeValue('mykey', $(this).val());
})

function storeValue(key, value){
    $.cookie(key, value)
}

function getValue(key){
    return $.cookie(key);
}

$(function(){
    var val = getValue('mykey') || 'Full Name';
    $('select option[value="' + val + '"]').prop("selected",true);
});

这篇关于防止选择选项更改回默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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