2015-10-14 153 views
2

做一點研究我現在知道我必須編寫所選選項的cookie。然後讀取該cookie以檢索該值。Javascript記住從下拉菜單中選擇的選項值

我發現了一個類似的問題,但無法弄清楚如何從答案

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> 

我設法創建一個下拉菜單,這將改變IFRAME SRC執行代碼。我只需要它來記住他們在刷新或瀏覽器退出時選擇的內容。

回答

1

對於這個當用戶選擇保值的價值,您的瀏覽器是本地/或會話存儲,當用戶重新打開先檢查本地存儲是否具有價值或者如果不使用jQuery是選擇價值。

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

你可以看到如何在W3School使用本地存儲: 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> 
+0

我知道我要存儲值問題我不知道如何使用表格 它會是這樣的? //商店 localStorage.setItem( 「selectedStream」, 「值」); //檢索 document.getElementById(「stream_iframe」)。innerHTML = localStorage.getItem(「selectedStream」); – Yami

+0

我已添加完整的代碼。將其複製到html文件並在瀏覽器上運行。 – amitguptageek

+0

謝謝你通過代碼看我現在更好地理解它現在謝謝你。 src在第一次運行時變爲空。 – Yami