在刷新之后保持选择的选项

时间:2022-09-25 15:00:03

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

我有一个按我在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.

我在网上看到一些帖子有问题,但大多数都使用JQuery,我不想使用它。

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

这段代码是用PHP手工生成的,但是我稍后会做一个函数

2 个解决方案

#1


6  

One way is to use javascript and localStorage:

一种方法是使用javascript和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.

您还可以在服务器端使用cookie或会话。

#2


0  

Most direct approach would be to store the values of $_POST[] in $_SESSION[].

最直接的方法是将$_POST[]的值存储在$_SESSION[]中。

#1


6  

One way is to use javascript and localStorage:

一种方法是使用javascript和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.

您还可以在服务器端使用cookie或会话。

#2


0  

Most direct approach would be to store the values of $_POST[] in $_SESSION[].

最直接的方法是将$_POST[]的值存储在$_SESSION[]中。