刷新后选择保持选项 [英] keep option selected after a refresh

查看:103
本文介绍了刷新后选择保持选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按照2个参数排序的人员列表,我在select中插入。



如何在有人选择参数时,在页面刷新或更改/返回时保持选择该参数?

我在互联网上发现了一些帖子,但大多数使用JQuery,我不想使用它。



代码是像这样:

 < select name =idactivity_contact []id =_ activitymultiple =multiplesize = 10style =width:150px;> 
< option disabled =disabledstyle =background-color:#dddddd; font-weight:bold;> Shipbroking< / option>
< option value =1>新建< / option>
< option value =2>销售和购买< / option>
< option value =3>批量< / option>
< / select>

代码在PHP中以手动方式生成,但我稍后会执行一个函数

解决方案一种方法是使用 javascript localStorage

$ p $ document.getElementById(_ activity)。onchange = function(){
localStorage。 setItem('selectedtem',document.getElementById(_ activity).value); (localStorage.getItem('item')){
document.getElementById(selectedtem)。options [localStorage.getItem('selectedtem')]。
}

if selected = true;
}

您也可以使用 cookies 会话在您的服务器端也是如此。


I have a list of people sorted by 2 parameters that I insert in a select.

How can, when someone select a parameter, maintain this one selected when he refresh or change/return on the page?

I see some issues on some post over the internet, but most of them use JQuery, and I don't want to use it.

The code is like this:

<select name="idactivity_contact[]" id="_activity" multiple="multiple" size="10" style="width:150px;">
    <option disabled="disabled" style="background-color:#dddddd;font-weight:bold;">Shipbroking</option>
    <option value="1">Newbuilding</option>
    <option value="2">Sales and Purchase</option>
    <option value="3">Bulk</option>
</select>

The code is generated in PHP actyally manually, but i will do a function later

解决方案

One way is to use javascript and localStorage:

document.getElementById("_activity").onchange = function() {
    localStorage.setItem('selectedtem', document.getElementById("_activity").value);
}

if (localStorage.getItem('item')) {
    document.getElementById("selectedtem").options[localStorage.getItem('selectedtem')].selected = true;
}​

You can also use cookies or session in your sever side as well.

这篇关于刷新后选择保持选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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