2011-01-15 6 views
0

Ich habe 3 Pulldown-Menüs auf der Seite. Nach der Auswahl des Benutzers wird das Bild und die Bildunterschrift geändert. Das Folgende ist ein Teil einer OnLoad-Funktion, die funktioniert. Ich suche jedoch nach einer Lösung, um diese 3 im Wesentlichen gleichen Funktionen zu kombinieren.So vereinfachen Sie Array onchange Funktion

Ich versuchte verschiedene Möglichkeiten, Variable als Array-Index z. myChoice [x] .onchange mit for (x in array) usw., kann aber nicht funktionieren.

myChoice ist ein Select-Element-Array und der erforderliche Wert wird für Bild und Beschriftung daraus extrahiert. myThumb ist ein Image-Element-Array. myDesc ist ein Span-Element-Array für die Beschriftung.

Ich würde wirklich einen guten Input schätzen. Vielen Dank!

myChoice [0] .onchange = function() {

jpgfile = this.value;
filechar = jpgfile.length-3;
shortfile = jpgfile.sub (filechar, 3);
myThumb [0] .src = bp + 'Trüffel' + Kurzdatei + '. Jpg';
captext = shortfile.sub (2,1);
myDesc [0] .innerHTML = Beschriftung [Captext];
}

myChoice [1] .onchange = function() {

jpgfile = this.value;
filechar = jpgfile.length-3;
shortfile = jpgfile.sub (filechar, 3);
myThumb [1] .src = bp + 'Trüffel' + Kurzdatei + '. Jpg';
captext = shortfile.sub (2,1);
myDesc [1] .innerHTML = Bildunterschrift [Captext];
}

myChoice [2] .onchange = function() {
jpgfile = this.value;
filechar = jpgfile.length-3;
shortfile = jpgfile.sub (filechar, 3);
myThumb [2] .src = bp + 'Trüffel' + Kurzdatei + '. Jpg';
captext = shortfile.sub (2,1);
myDesc [2] .innerHTML = Bildunterschrift [Captext]; }

GESAMTE CODE MIT SUGGESTED FUNKTIONEN

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 

<html> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<style type="text/css"> 
img.choicePic { 
display:block; 
} 
h1 { 
font-family:arial, Verdana, tahoma; 
font-size:17pt; 
text-align:center; 
display:block; 
} 
#caption0, #caption1, #caption2 { 
font-family:arial, Verdana, tahoma; 
font-size:7pt; 
text-align:left; 
display:block; 
} 
</style> 
<script type="text/javascript"> 

function setPicCap() { 

var caption=['Select your chocolate', 
'Flavors from India: nutmeg and subtle spices, in milk chocolate, dusted with cinnamon. ', 
'Caribbean flavors of rum and butter mingle in a creamy center, dipped in dark chocolate. ', 
'Dark chocolate truffle center, dipped in dark chocolate with white chocolate stripes. ', 
]; // This will be your images description 




var bp='images/store/', //base url of your images 
imgnum=3; //Number of your images. This should match on your comboboxes options. 

var myThumb=new Array(3), myDesc=new Array(3), myChoice=new Array(3); 

for (i=0; i<imgnum; i++) { 

numChoice=i.toString(); 
imgChoice="thumb"+numChoice; 
capChoice="caption"+numChoice; 
selectChoice="selection"+numChoice; 

imgChoice=document.getElementById(imgChoice); //Image element. Use .src to view specific. 
myThumb[i]=imgChoice; 


capChoice=document.getElementById(capChoice); //Span element. Place holder. No value. 
myDesc[i]=capChoice; 

selectChoice=document.getElementById(selectChoice); //Select element. Use innerHTML to view specific. 
myChoice[i]=selectChoice; 

} 

function updateThumb(index, jpgfile){ 
shortfile = jpgfile.substr(jpgfile.length - 3,3); 
myThumb[index].src = bp + 'truffle' + shortfile + '.jpg'; 
captext = shortfile.substr(2,1); 
myDesc[index].innerHTML = caption[captext]; 
} 

for(var j = 0; j < myChoice.length; j++){ 
var idx = j; 
myChoice.onchange = function(){ 
updateThumb(idx, this.value); 
} 
} 
} 


</script> 
</head> 
<body onLoad="setPicCap()"> 
<center> 
<h1>Build Your Computer Test Script</h1> 
</center> 
<table width="800" align="center"> 
<tr> 
<td><img src="images/store/select_a_chocolate.png" alt="Mukilteo Choclate Truffle" border="0" id="thumb0" class="choicePic"></td> 
<td><select id="selection0" name="truffle"> 
<option value="0" selected >Select Your Chocolate</option> 
<option value="Square0: BANGALORE SPICE Item 001|BANGALORE SPICE Item 001">BANGALORE SPICE Item 001</option> 
<option value="Square0: BUTTERED RUM Item 002|BUTTERED RUM Item 002">BUTTERED RUM Item 002</option> 
<option value="Square0: CLASSIC DARK CHOCOLATE Item 003|CLASSIC DARK CHOCOLATE Item 003">CLASSIC DARK CHOCOLATE Item 003</option> 
</select> 
<br></td> 
</tr> 
<tr> 
<td colspan="2"><span id="caption0"></span></td> 
</tr> 
</table> 
<br> 
<br> 
<br> 
<table width="800" align="center"> 
<tr> 
<td><img src="images/store/select_a_chocolate.gif" alt="Mukilteo Choclate Truffle" border="0" id="thumb1" class="choicePic"></td> 
<td><select id="selection1" name="truffle"> 
<option value="0" selected >Select Your Chocolate</option> 
<option value="Square1: BANGALORE SPICE Item 001|BANGALORE SPICE Item 001">BANGALORE SPICE Item 001</option> 
<option value="Square1: BUTTERED RUM Item 002|BUTTERED RUM Item 002">BUTTERED RUM Item 002</option> 
<option value="Square1: CLASSIC DARK CHOCOLATE Item 003|CLASSIC DARK CHOCOLATE Item 003">CLASSIC DARK CHOCOLATE Item 003</option> 
</select> 
<br></td> 
</tr> 
<tr> 
<td colspan="2"><span id="caption1"></span></td> 
</tr> 
</table> 
<br> 
<br> 
<br> 
<table width="800" align="center"> 
<tr> 
<td><img src="images/store/select_a_chocolate.jpg" alt="Mukilteo Choclate Truffle" border="0" id="thumb2" class="choicePic"></td> 
<td><select id="selection2" name="truffle"> 
<option value="0" selected >Select Your Chocolate</option> 
<option value="Square2: BANGALORE SPICE Item 001|BANGALORE SPICE Item 001">BANGALORE SPICE Item 001</option> 
<option value="Square2: BUTTERED RUM Item 002|BUTTERED RUM Item 002">BUTTERED RUM Item 002</option> 
<option value="Square2: CLASSIC DARK CHOCOLATE Item 003|CLASSIC DARK CHOCOLATE Item 003">CLASSIC DARK CHOCOLATE Item 003</option> 
</select> 
<br></td> 
</tr> 
<tr> 
<td colspan="2"><span id="caption2"></span></td> 
</tr> 
</table> 
</body> 
</html> 

Antwort

1
function updateThumb(index, jpgfile){ 
    shortfile = jpgfile.substr(jpgfile.length - 3,3); 
    myThumb[index].src = bp + 'truffle' + shortfile + '.jpg'; 
    captext = shortfile.substr(2,1); 
    myDesc[index].innerHTML = caption[captext]; 
} 

for(var i = 0; i < myChoice.length; i++){ 
    var idx = i; 
    myChoice.onchange = function(){ 
     updateThumb(idx, this.value); 
    } 
} 
+0

Sie für unsere schnelle Antwort danken, The Scrum Meister. Es sah sehr vielversprechend aus, hat aber nicht funktioniert. Ich weiß nicht warum. Ich denke darüber nach, es in jedem Select-Tag zu tun, indem es den Array-Index übergibt, anstatt von onLoad aus aufzurufen. Ich habe den gesamten Code zu meiner ursprünglichen Frage hinzugefügt. – tkl

0

Hier gehen Sie:

function setPicCap() { 
function a(a, d) { 
    shortfile = d.substr(d.length - 3, 3), e[a].src = c + "truffle" + shortfile + ".jpg", captext = shortfile.substr(2, 1), f[a].innerHTML = b[captext] 
} 
var b = ["Select your chocolate", "Flavors from India: nutmeg and subtle spices, in milk chocolate, dusted with cinnamon. ", "Caribbean flavors of rum and butter mingle in a creamy center, dipped in dark chocolate. ", "Dark chocolate truffle center, dipped in dark chocolate with white chocolate stripes. "], 
    c = "images/store/", 
    d = 3, 
    e = Array(3), 
    f = Array(3), 
    g = Array(3); 
for (i = 0; d > i; i++) numChoice = i + "", imgChoice = "thumb" + numChoice, capChoice = "caption" + numChoice, selectChoice = "selection" + numChoice, imgChoice = document.getElementById(imgChoice), e[i] = imgChoice, capChoice = document.getElementById(capChoice), f[i] = capChoice, selectChoice = document.getElementById(selectChoice), g[i] = selectChoice; 
for (var h = 0; g.length > h; h++) { 
    var j = h; 
    g.onchange = function() { 
     a(j, this.value) 
    } 
} 
}