我正在尝试选择下拉列表的值并将变量传递给javascript,这是有效的,但是即使页面刷新时我也试图保存所选选项的值
我想我可以在选定的值上创建一个cookie并检查它是否存在。
任何帮助将不胜感激
形式
<form name="dummyForm">
<select name="selectBox" onchange="getSelectedvalue()">
<option value="option 1">option 1</option>
<option value="option 2">option 2</option>
<option value="option 3">option 3</option>
<option value="option 4">option 4</option>
<option value="option 5">option 5</option>
<option value="option 6">option 6</option>
<option value="option 7">option 7</option>
<option value="option 8">option 8</option>
</select>
</form>
<a href="javascript:setSelectBoxvalue()">Click me</a>
的javascript
<script type="text/javascript">
function getSelectedvalue()
{
var getSelectedIndex = document.dummyForm.selectBox.selectedIndex;
var getSelectedOptionText = document.dummyForm.selectBox[getSelectedIndex].text;
alert(getSelectedOptionText);
}
function setSelectBoxvalue()
{
var len = document.dummyForm.selectBox.length;
//alert(len);
var optionText= 'option 1';
for(var i=0; i<=len; i++){
if( (document.dummyForm.selectBox[i].value) == optionText){
alert(document.dummyForm.selectBox[i].text);
document.dummyForm.selectBox.selectedIndex = i;
}
}
}
</script>
答案 0 :(得分:3)
如果有HTML5支持使用localStorage
if(typeof(Storage)!=="undefined")
{
// Yes! localStorage and sessionStorage support!
localStorage.paramName="value";
}
else
{
// Sorry! No web storage support..
}
然后就这样:
var myParam = localStorage.paramName;
如果没有HTML5支持,请使用cookies:
设置jQuery jQuery:
$.cookie("paramName", "value");
使用jQuery获取
$.cookie("paramName");
没有jQuery(虽然我不知道为什么不使用jQuery:P)http://www.w3schools.com/js/js_cookies.asp:
function setCookie(c_name, value, exdays)
{
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start, c_end));
}
return c_value;
}
答案 1 :(得分:3)
您可以使用有用的博客QuirksMode提供的功能轻松操作Cookie:
http://www.quirksmode.org/js/cookies.html
以下是如何实现您的具体案例(例如,您为选择'selectBox'指定了ID):
window.onload = function()
{
var cookieVal = readCookie('selectBoxValue');
if(cookieVal!=null)
{
document.getElementById('selectBox').selectedIndex = cookieVal;
}
}
function changeSelectedvalue()
{
var mySelectedIndex = document.getElementById('selectBox').selectedIndex;
createCookie("selectBoxValue",mySelectedIndex,1000);
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
请看这里的小提琴:
http://jsfiddle.net/SpAm/9VmQD/
这里的全屏版本: