JavaScript如何记住下拉菜单中所选择的选项值

3

经过一些研究,我现在知道我需要编写所选选项的 cookie。然后读取 cookie 以检索值。

我找到了一个类似的问题,但是无法理解如何实现答案中的代码。

问题链接

<html>
<head>
<title>dropdown remember selection test</title>
</head>
<body>

<iframe id="stream_iframe" marginwidth="0" marginheight="0" width="854"   height="480" scrolling="no" allowtransparency="true" frameborder="0" framespacing="0" name="stream_iframe" src="http://vaughnlive.tv/embed/video/moviebay"></iframe> 

<form> 
<select name="options" onchange="document.getElementById('stream_iframe').src = this.options[this.selectedIndex].value">
<option>SELECT STREAM
<option value="https://streamup.com/rooms/jumanjijoes-Channel/plugins/video/show?startMuted=false">Jumanjijoe</option>
<option value="http://vaughnlive.tv/embed/video/whenlinkattacks">Whenlinkattacks</option>
<option value="https://streamup.com/rooms/docblus-Channel/plugins/video/show?startMuted=false">Docblu</option> 
<option value="http://vaughnlive.tv/embed/video/karenwaifu">Karenmaiwaifu</option>
<option value="http://vaughnlive.tv/embed/video/ninjatart">Nightsanity</option>
<option value="http://vaughnlive.tv/embed/video/frightfest0001">Frightfest0001</option> 
<option value="http://vaughnlive.tv/embed/video/anime_hq">Anime HQ</option>
<option value="http://vaughnlive.tv/embed/video/moviebay">MovieBay</option>
<option value="http://vaughnlive.tv/embed/video/freakyfetish101">Horror Movie</option>
<option value="http://vaughnlive.tv//embed/video/111aaacharkmovies">111aaacharkmovies </option> 
</select>
</form>

</body>
</html>

我成功地创建了一个下拉菜单,可以更改iframe SRC。我只需要它在刷新或浏览器退出时记住用户选择的选项。
1个回答

1
用户选择值时,将值存储到您的浏览器的本地或会话存储中。当用户重新打开页面时,首先检查本地存储是否存在该值,如果存在,则使用jQuery选择该值。
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

你可以在w3schools上查看如何使用本地存储: http://www.w3schools.com/html/html5_webstorage.asp 编辑:
在这里找到完整的代码。
<iframe id="stream_iframe" marginwidth="0" marginheight="0" width="854" height="480" scrolling="no" allowtransparency="true" frameborder="0" framespacing="0" name="stream_iframe" src="http://vaughnlive.tv/embed/video/moviebay"></iframe> 

<form> 
<select name="options" onchange="callMe(this);" id="selectMovie">
<option>SELECT STREAM
<option value="https://streamup.com/rooms/jumanjijoes-Channel/plugins/video/show?startMuted=false">Jumanjijoe</option>
<option value="http://vaughnlive.tv/embed/video/whenlinkattacks">Whenlinkattacks</option>
<option value="https://streamup.com/rooms/docblus-Channel/plugins/video/show?startMuted=false">Docblu</option> 
<option value="http://vaughnlive.tv/embed/video/karenwaifu">Karenmaiwaifu</option>
<option value="http://vaughnlive.tv/embed/video/ninjatart">Nightsanity</option>
<option value="http://vaughnlive.tv/embed/video/frightfest0001">Frightfest0001</option> 
<option value="http://vaughnlive.tv/embed/video/anime_hq">Anime HQ</option>
<option value="http://vaughnlive.tv/embed/video/moviebay">MovieBay</option>
<option value="http://vaughnlive.tv/embed/video/freakyfetish101">Horror Movie</option>
<option value="http://vaughnlive.tv//embed/video/111aaacharkmovies">111aaacharkmovies </option> 
</select>
</form>
<script>
function callMe(obj){
localStorage.setItem("selectedStream",obj.options[obj.selectedIndex].value);
document.getElementById('stream_iframe').src = obj.options[obj.selectedIndex].value;
}
document.getElementById("stream_iframe").src = localStorage.getItem("selectedStream");
document.getElementById("selectMovie").value = ""+localStorage.getItem("selectedStream")+"";
</script>

我知道我需要存储这些值,但问题是我不知道如何使用表单来实现。可能会像这样吗?// 存储 localStorage.setItem("selectedStream", "value"); // 检索 document.getElementById("stream_iframe").innerHTML = localStorage.getItem("selectedStream"); - user1719826
我已经添加了完整的代码。将其复制到HTML文件中并在浏览器上运行。 - amitguptageek
谢谢您查看代码,现在我更好地理解了,谢谢。不过,在第一次运行时,src会变为空值。 - user1719826

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接