webForumDet fria alternativet

selectedIndex].value

1 svar · 333 visningar · startad av stephan

stephanMedlem sedan feb. 2006117 inlägg
#1

Jag försöker använda mig av ett script från Dynamic Drive som heter "Switch Menu". När man ändrar i en val i en select-box så ska det visas olika innehåll på sidan beroende på vad man valt. Det fungerar så långt, men en av valen i selectboxen kommer vara selected när man går in på sidan, och då vill jag såklart att innehållet ska visas för det aktuella valet.

Trodde att det här skulle funka:

onLoad="SwitchMenu(document.getElementById("infoType").options[document.getElementById("infoType").selectedIndex].value);"

Men icke. Vad har jag gjort för fel?

Hela koden:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
<!--
/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)
if (cookievalue!="")
document.getElementById(cookievalue).style.display="block"
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate
-->
</script>

<body id="mainBody" onLoad="SwitchMenu(document.getElementById("infoType").options[document.getElementById("infoType").selectedIndex].value);">
<form>
	<select id="infoType" name="info/infoType_cd" onChange="SwitchMenu(this.options[this.selectedIndex].value)">
		<option value="IMAGE">IMAGE</option>
		<option value="INCLUDE">INCLUDE</option>
		<option value="MENU">MENU</option>
		<option value="TEXT" selected>TEXT</option>
	</select>
</form>
<div id="masterdiv">

	<span class="submenu" id="IMAGE">
		Image options
	</span>

	<span class="submenu" id="INCLUDE">
		Include options
	</span>

	<span class="submenu" id="MENU">
		Menu options
	</span>
	
	<span class="submenu" id="TEXT">
		Text options
	</span>
</div>

</body>
</html>
stephanMedlem sedan feb. 2006117 inlägg
#2

Skrev om det lite själv och gjorde det enklare. Så nu funkar det:

<script type="text/javascript">
<!--

function adminInfoSwitchDisplay(obj) {
	if(document.getElementById) {
		if (obj == 'IMAGE') {
			document.getElementById('imageInfoContainer').style.display = "block";
			document.getElementById('textInfoContainer').style.display = "none";
			document.getElementById('linkProperties').style.display = "block";
		}
		else if (obj == 'TEXT') {
			document.getElementById('imageInfoContainer').style.display = "none";
			document.getElementById('textInfoContainer').style.display = "block";
			document.getElementById('linkProperties').style.display = "block";
		}
		else if (obj == 'INCLUDE') {
			document.getElementById('imageInfoContainer').style.display = "none";
			document.getElementById('textInfoContainer').style.display = "none";
			document.getElementById('linkProperties').style.display = "none";
		}
		else if (obj == 'MENU') {
			document.getElementById('imageInfoContainer').style.display = "none";
			document.getElementById('textInfoContainer').style.display = "none";
			document.getElementById('linkProperties').style.display = "block";
		}
	}
}
-->
</script>

<body id="mainBody" onLoad="adminInfoSwitchDisplay(document.getElementById('testSelect').options[document.getElementById('testSelect').selectedIndex].value);">
135 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)