简体   繁体   中英

HTML JAVASCRIPT or PHP how to make options in select dependent on option selected in another select

I have two selects

<select name="firstselect" id="firstselect">
<option value="optionOne">optionOne</option>
<option value="optionTwo">optionTwo</option>
</select>

<select name="secondselect" id="secondselect">
<option value="secondOptionOne">secondOptionOne</option>
<option value="secondOptionTwo">secondOptionTwo</option>
<option value="secondOptionThree">secondOptionThree</option>
<option value="secondOptionFour">secondOptionFour</option>
<option value="secondOptionFive">secondOptionFive</option>  
</select>

I want the options in the secondselect to change based on the option the user chooses in the first select. How can I do this either in Javascript or PHP?

I found a nice solution to this here: http://www.webdeveloper.com/forum/showthread.php?91848-How-do-I-make-one-lt-select-gt-list-dependent-on-the-selection-made-in-another

It's not my code but I hope it helps.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>select change 2nd select</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript">
    var varieties=[
    ["varieties","granny smith","golden delicious","jonathan"],
    ["varieties","anjou","bartlett","conference"],
    ["varieties","valencia","pineapple","pera"]
    ];

    function Box2(idx) {
    var f=document.myform;
    f.box2.options.length=null;
    for(var i=0; i<varieties[idx].length; i++) {
        f.box2.options[i]=new Option(varieties[idx][i], i); 
        }    
    }
    </script>

    </head>
    <body onload="Box2(0);">
    <form name="myform" method="post" action="http://www.mysite.com/mysite">
    <fieldset>
    <select name="box1" onchange="Box2(this.selectedIndex)">
        <option value="a">apple</option>
        <option value="b">pear</option>
        <option value="c">orange</option>
    </select>
    <select name="box2">

    </select>
    </fieldset>
    </form>
    </body>
    </html>

I have had the same problem as you. The following javascript, written inside the space corresponding to the <head> tag, could be useful if both lists have the same elements and the first element of the list "firstselect" is related to the first one of the list "secondselect", the second element of the list "firstselect" is related to the second one of the list "secondselect" and so on.

<script language="javascript" type="text/javascript">
// <!CDATA[
function Select1_onclick() {
    var SelectList1 = document.getElementById('firstselect');
    var SelectList2 = document.getElementById('secondselect');
    SelectL2.selectedIndex = LSelectL1.selectedIndex;
// ]]>
</script>

You also should modify the <select> tag for the list "firstselect" to include the above script in this way:

<select name="firstselect" id="firstselect" onclick="Select1_onclick()">

If you pretend to make a different options choice, you could use a switch-case structure (see a example here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM