Javascript记住从下拉菜单中选择的选项值 [英] Javascript remember selected option value from dropdown menu

查看:63
本文介绍了Javascript记住从下拉菜单中选择的选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行一些研究,现在我知道我必须编写所选选项的cookie.然后读取Cookie以获取该值.

Doing a bit of research I now know I have to write the cookie of the selected option. then read the cookie to retrieve the value.

我发现了一个类似的问题,但无法从答案中找出如何实现代码

I found a similar question but couldn't figure out how to implement the code from the answer

链接到问题

<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.我只需要记住它们在刷新或浏览器退出时选择的内容即可.

I managed to create a dropdown menu that will change the iframe SRC. I just need it to remember what they select on refresh or browser exit.

推荐答案

为此,当用户选择值存储时,将值存储到浏览器的本地/或会话存储中,并且当用户重新打开时,首先检查本地存储是否具有该值或如果是,则不使用jquery选择该值.

For this when user selects the value store the value to either local/or session storage of your browser and when user reopens first check whether local storage has the value or not if yes select the value using jquery.

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

您可以在w3schools上了解如何使用本地存储: http://www.w3schools.com/html/html5_webstorage.asp

You can see how to use local storage at 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>

这篇关于Javascript记住从下拉菜单中选择的选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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