简体   繁体   English

Javascript记住从下拉菜单中选择的选项值

[英]Javascript remember selected option value from dropdown menu

Doing a bit of research I now know I have to write the cookie of the selected option. 做一些研究我现在知道我必须编写所选选项的cookie。 then read the cookie to retrieve the value. 然后读取cookie以检索值。

I found a similar question but couldn't figure out how to implement the code from the answer 我发现了一个类似的问题,但无法弄清楚如何从答案中实现代码

Link to question 链接到问题

<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>

I managed to create a dropdown menu that will change the iframe SRC. 我设法创建了一个下拉菜单,它将改变iframe SRC。 I just need it to remember what they select on refresh or browser exit. 我只需要记住他们在刷新或浏览器退出时选择的内容。

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. 为此,当用户选择值时,将值存储到浏览器的本地/或会话存储中,并在用户重新打开时首先检查本地存储是否具有该值,如果是,则使用jquery选择值。

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

You can see how to use local storage at w3schools: http://www.w3schools.com/html/html5_webstorage.asp 你可以在w3schools看到如何使用本地存储: http//www.w3schools.com/html/html5_webstorage.asp

Edited: 编辑:

Find Complete code Here 在此查找完整代码

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM