Kleines Problem

Lektor21

Erfahrenes Mitglied
Hallo!

Wie muss man das folgende Script umschreiben, dass anstatt 0,1 und 2
-> no, yes, auto funktioniert?

HTML:
<html>
<head>
<script>
function ShowMenu(num, menu, max)
        {
                
                for(i = 1; i <= num; i++){
                        //add number onto end of menu
                        var menu2 = menu + i;
                        //change visibility to block, or 'visible'
                        document.getElementById(menu2).style.display = 'block';
                }
                //make a number one more than the number inputed
                var num2 = num;
                num2++;
                //hide it if the viewer selects a number lower
                //this will hide every number between the selected number and the maximum
                //ex.  if 3 is selected, hide the <div> cells for 4, 5, and 6
                //loop until max is reached
                while(num2 <= max){
                        var menu3 = menu + num2;
                        //hide 
                        document.getElementById(menu3).style.display = 'none';
                        //add one to loop
                        num2=num2+1;
                }
        }
</script>
</head>

<body>



<select id='wartung' onChange="javascript: ShowMenu(document.getElementById('wartung').value,'div-wartung', 2);">
        <option value='0'>0
        <option value='1'>1
        <option value='2'>2
</select>


<div id='div-wartung1' style="display: none;">
                Choose type of flower 1:<br><br>
                <input type="radio" name="color1" value="red">Red<br>
                <input type="radio" name="color1" value="white">White<br>
                <input type="radio" name="color1" value="yellow">Yellow<br>
</div>

<div id='div-wartung2' style="display: none;">
                Choose type of flower 2:<br><br>
                <input type="radio" name="color2" value="red">Red<br>
                <input type="radio" name="color2" value="white">White<br>
                <input type="radio" name="color2" value="yellow">Yellow<br>


Also auf folgendem;


HTML:
<select id='wartung' onChange="javascript: ShowMenu(document.getElementById('wartung').value,'div-wartung', 2);">
        <option value='0'>0
        <option value='1'>1
        <option value='2'>2
</select>


soll werden;


HTML:
<select id='wartung' onChange="javascript: ShowMenu(document.getElementById('wartung').value,'div-wartung', 2);">
        <option value='no'>No
        <option value='yes'>Yes
        <option value='auto'>Auto
</select>


Und die funktionsweise soll weiterhin bestehen bleiben :confused:
 
Zuletzt bearbeitet:
Hab nun ein Script gefunden, was funktioniert!

HTML:
function setVisibility() 
{

var val=document.wartung.wartung_updated.selectedIndex;

if(val){

if(val == 0){
option1_part1.style.display = 'none';
option1_part2.style.display = 'none';
option2_part1.style.display = 'none';
option2_part2.style.display = 'none';
}

if(val == 1){
option1_part1.style.display = 'none';
option1_part2.style.display = 'none';
option2_part1.style.display = 'block';
option2_part2.style.display = 'block';
}

if(val == 2){
option1_part1.style.display = 'block';
option1_part2.style.display = 'block';
option2_part1.style.display = 'block';
option2_part2.style.display = 'block';
}

}

}

Nur noch nen kleines Problem; wenn ich in der Liste den ersten auswähle blendet er nicht alle aus, wie ich aber eigentlich definiert habe!? :confused:
 
Code:
if(val){

if(val == 0){
if(val)...prüft, ob val true ist
wenn val allerdings 0 ist(falls erste Option gewählt wurde)...dann kommst du überhaupt nicht in den Abzweig, weil val dann false ist.(bei einer Prüfung auf einen booleschen Wert wird 0 als false gewertet)
 
Ok hab entfernt! Dankeschön, jetzt funktioniert es!

Also ich hole den Inhalt vorher aus einer Datenbank und je nachdem checke ich das Selectfeld, also entweder no, yes oder auto.
Ist es eigentlich auch möglich, dass wenn man auf die Seite kommt, dass automatisch eben die Felder ausgeblendet werden, die nicht hingehören?

Weil wenn ich nichts anklicke sieht man ja alle DIVS! :confused:
 

Neue Beiträge

Zurück