同时在页面上显示级联列表

时间:2014-12-11 06:34:11

标签: javascript php jquery html drop-down-menu

我有级联下拉列表的代码,这有助于执行搜索。

它完美地运行并显示结果,但问题是,

首次加载页面时,会显示第一个下拉列表,当从中选择一个值时,将显示第二个下拉列表,并在从第二个下拉列表中选择一个值后显示执行搜索。

我希望在下拉列表选择时,下拉列表应该一起显示,

但是,第二个下拉列表应该包含其中的任何值,或者可以被禁用(首选),并且仅当从第一个下拉列表中选择一个值时才会启用。

的index.php

    <body>
    <div id="container">
        <div id="body">
            <div class="mhead"><h2></h2></div>
            <div id="dropdowns">
                <div id="center" class="cascade">
                    <?php
                    $sql = "SELECT * FROM search_parent ORDER BY searchname";
                    $query = mysqli_query($con, $sql);
                    ?>
                    <label>Country:
                        <select name="country" id = "drop1">
                            <option value="">Please Select</option>
                            <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
                                <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["searchname"]; ?></option>
                            <?php } ?>
                        </select>
                    </label>
                </div>
                <div class="cascade" id="state"></div> 
                <div id="city" class="cascade"></div> 
            </div>
        </div>
    </div>
    <div class="showsearch"> </div>
    <p>&nbsp;</p>
    <script src="jquery-1.11.1.js"></script>
    <script>
        $(document).ready(function (){
            $("select#drop1").change(function (){
                var parent_id = $("select#drop1 option:selected").attr('value');
                // alert(parent_id);    
                $("#state").html("");
                //$("#city").html( "" );
                if (parent_id.length > 0)
                {
                    $.ajax({
                        type: "POST",
                        url: "fetch_state.php",
                        data: "parent_id=" + parent_id,
                        cache: false,
                        beforeSend: function ()
                        {
                            $('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
                        },
                        success: function (html)
                        {
                            $("#state").html(html);
                        }
                    });
                }
            });
        });
    </script>
</body>

fetch_state.php

    <?php
include("connection.php");
$parent_id = trim(mysql_escape_string($_POST["parent_id"]));

$sql = "SELECT * FROM features WHERE parent_id = '" . $parent_id . "' ORDER BY fname";
$count = mysqli_num_rows(mysqli_query($con, $sql));
if ($count > 0) {
    $query = mysqli_query($con, $sql);
    ?>
    <label>State: 
        <select name="state" id="drop2">
            <option value="">Please Select</option>
            <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
                <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["fname"]; ?></option>
            <?php } ?>
        </select>
    </label>
<?php } ?>
<script src="jquery-1.11.1.js"></script>
<script>
    $(document).ready(function () {
        $('#drop2').on('change', function () {
            //var fname = $(this).val();
            var fname = $(this).find('option:selected').text();
            // rename your file which include $fname  with get_search_data.php
            if (fname !== "") {
                $.post('get_search_data.php', {fname: fname}, function (data) {<!--get_search_data.php is used to perform search--->
                    $('.showsearch').html(data);
                });
            }
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

将这两个下拉列表放在index.php中,但将第二个下拉列表设为禁用属性。同时给它一个值:

<select name="state" id="drop2" disabled="true">
<option value="">Please Select</option>
</select>

现在将以下内容放在$(document).ready

$('#drop1').change(function(){
    $('#drop2').prop("disabled", false);
});

这将禁用第二个下拉列表,直到第一个下拉列表选择一个值 将来自fetch_state.php的JS放在其余部分之下。 现在,当您选择一个值时,将启用下拉列表并填充该下拉列表。

结果应如下所示:

<body>
<div id="container">
    <div id="body">
        <div class="mhead"><h2></h2></div>
        <div id="dropdowns">
            <div id="center" class="cascade">
                <?php
                $sql = "SELECT * FROM search_parent ORDER BY searchname";
                $query = mysqli_query($con, $sql);
                ?>
                <label>Country:
                    <select name="country" id = "drop1">
                        <option value="">Please Select</option>
                        <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
                            <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["searchname"]; ?></option>
                        <?php } ?>
                    </select>
                </label>
            </div>
            <div class="cascade" id="state">
             <label>State: 
                <select name="state" id="drop2" disabled="true">
                <option value="">Please Select</option>
                </select>
            </label>
            </div> 
            <div id="city" class="cascade"></div> 
        </div>
    </div>
</div>
<div class="showsearch"> </div>
<p>&nbsp;</p>
<script src="jquery-1.11.1.js"></script>
<script>
    $(document).ready(function (){
        $("select#drop1").change(function (){
            var parent_id = $("select#drop1 option:selected").attr('value');
            // alert(parent_id);    
            $("#state").html("");
            //$("#city").html( "" );
            if (parent_id.length > 0)
            {
                $.ajax({
                    type: "POST",
                    url: "fetch_state.php",
                    data: "parent_id=" + parent_id,
                    cache: false,
                    beforeSend: function ()
                    {
                        $('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
                    },
                    success: function (html)
                    {
                        $("#state").html(html);
                    }
                });
            }
        });

         $('.drop1').change(function(){
            $('.drop2').prop("disabled", false);
         });

         $('#drop2').on('change', function () {
        //var fname = $(this).val();
        var fname = $(this).find('option:selected').text();
        // rename your file which include $fname  with get_search_data.php
        if (fname !== "") {
            $.post('get_search_data.php', {fname: fname}, function (data) {<!--get_search_data.php is used to perform search--->
                $('.showsearch').html(data);
            });
        }
    });
    });
</script>
</body>