Drop-down box dependent on the option selected in another drop-down box

后端 未结 7 1760
半阙折子戏
半阙折子戏 2020-11-29 05:55

I have 2 different SELECT OPTION in a form.

The first one is Source, the second one is Status. I would like to have different OPTIONS in my Status drop-down list dep

相关标签:
7条回答
  • 2020-11-29 06:33

    You're better off making two selects and showing one while hiding the other.

    It's easier, and adding options to selects with your method will not work in IE8 (if you care).

    0 讨论(0)
  • 2020-11-29 06:38

    for this, I have noticed that it far better to show and hide the tags instead of adding and removing them for the DOM. It performs better that way.

    0 讨论(0)
  • 2020-11-29 06:46
        function dropdownlist(listindex)
        {
             document.getElementById("ddlCity").options.length = 0;
             switch (listindex) 
             {
                 case "Karnataka":
                         document.getElementById("ddlCity").options[0] = new Option("--select--", "");
                         document.getElementById("ddlCity").options[1] = new Option("Dharawad", "Dharawad");
                         document.getElementById("ddlCity").options[2] = new Option("Haveri", "Haveri");
                         document.getElementById("ddlCity").options[3] = new Option("Belgum", "Belgum");
                         document.getElementById("ddlCity").options[4] = new Option("Bijapur", "Bijapur");
    
                     break;
    
                 case "Tamilnadu":
                     document.getElementById("ddlCity").options[0] = new Option("--select--", "");
                     document.getElementById("ddlCity").options[1] = new Option("dgdf", "dgdf");
                     document.getElementById("ddlCity").options[2] = new Option("gffd", "gffd");
    
    
                     break;
             }
    
        }
    

    * State: --Select-- Karnataka Tamilnadu Andra pradesh Telngana

        <div>
            <p>
    
                <label id="lblCt">
                <span class="red">*</span>
                    City:</label>
                <select id="ddlCity">
                   <!-- <option>--Select--</option>
                    <option value="1">Dharawad</option>
                    <option value="2">Belgum</option>
                    <option value="3">Bagalkot</option>
                    <option value="4">Haveri</option>
                    <option>Hydrabadh</option>
                    <option>Vijat vada</option>-->
                </select>
                <label id="lblCity"></label>
            </p>
        </div>
    
    0 讨论(0)
  • 2020-11-29 06:50

    Try something like this... jsfiddle demo

    HTML

    <!-- Source: -->
    <select id="source" name="source">
         <option>MANUAL</option>
         <option>ONLINE</option>
    </select>
    
    <!-- Status: -->
    <select id="status" name="status">
        <option>OPEN</option>
        <option>DELIVERED</option>
    </select>
    

    JS

    $(document).ready(function () {
    
        $("#source").change(function () {
            var el = $(this);
            if (el.val() === "ONLINE") {
                $("#status").append("<option>SHIPPED</option>");
            } else if (el.val() === "MANUAL") {
                $("#status option:last-child").remove();
            }
        });
    
    });
    
    0 讨论(0)
  • 2020-11-29 06:50

    I am posting this answer because in this way you will never need any plugin like jQuery and any other, This has the solution by simple javascript.

    <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script language="javascript" type="text/javascript">
        function dynamicdropdown(listindex)
        {
            switch (listindex)
            {
            case "manual" :
                document.getElementById("status").options[0]=new Option("Select status","");
                document.getElementById("status").options[1]=new Option("OPEN","open");
                document.getElementById("status").options[2]=new Option("DELIVERED","delivered");
                break;
            case "online" :
                document.getElementById("status").options[0]=new Option("Select status","");
                document.getElementById("status").options[1]=new Option("OPEN","open");
                document.getElementById("status").options[2]=new Option("DELIVERED","delivered");
                document.getElementById("status").options[3]=new Option("SHIPPED","shipped");
                break;
            }
            return true;
        }
        </script>
        </head>
        <title>Dynamic Drop Down List</title>
        <body>
        <div class="category_div" id="category_div">Source:
            <select id="source" name="source" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
            <option value="">Select source</option>
            <option value="manual">MANUAL</option>
            <option value="online">ONLINE</option>
            </select>
        </div>
        <div class="sub_category_div" id="sub_category_div">Status:
            <script type="text/javascript" language="JavaScript">
            document.write('<select name="status" id="status"><option value="">Select status</option></select>')
            </script>
            <noscript>
            <select id="status" name="status">
                <option value="open">OPEN</option>
                <option value="delivered">DELIVERED</option>
            </select>
            </noscript>
        </div>
        </body>
    </html>
    

    For more details, I mean to make dynamic and more dependency please take a look at my article create dynamic drop-down list

    0 讨论(0)
  • 2020-11-29 06:55

    I hope the following code will help or solve your problem or you think not that understandable visit http://phppot.com/jquery/jquery-dependent-dropdown-list-countries-and-states/.

    HTML DYNAMIC DEPENDENT SELECT

    <div class="frmDronpDown">
    <div class="row">
        <label>Country:</label><br/>
        <select name="country" id="country-list" class="demoInputBox" onChange="getState(this.value);">
        <option value="">Select Country</option>
        <?php
        foreach($results as $country) {
        ?>
        <option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
        <?php
        }
        ?>
        </select>
    </div>
    <div class="row">
        <label>State:</label><br/>
        <select name="state" id="state-list" class="demoInputBox">
        <option value="">Select State</option>
        </select>
    </div>
    

    GETTING STATES VIA AJAX

    <script> function getState(val) { $.ajax({
    type: "POST",
    url: "get_state.php",
    data:'country_id='+val,
    success: function(data){
        $("#state-list").html(data);
    }
    });} </script>
    

    READ STATE DATABASE USING PHP

    <?php require_once("dbcontroller.php"); $db_handle = new DBController();if(!empty($_POST["country_id"])) {
    $query ="SELECT * FROM states WHERE countryID = '" . $_POST["country_id"] . "'";
    $results = $db_handle->runQuery($query); ?> <option value="">Select State</option><?php foreach($results as $state) { ?> <option value="<?php echo $state["id"]; ?>"><?php echo $state["name"]; ?></option><?php } } ?>
    
    0 讨论(0)
提交回复
热议问题