州城下拉列表

时间:2014-07-31 12:17:18

标签: javascript

我测试了这个state city dropdown not working

州和城市的JavaScript代码下拉列表。我的问题是当这个编程运行时,显示来自相应状态的第一个城市,人们无法选择处于相同状态的另一个城市。  例如: 在马哈拉施特拉邦,展出的城市是孟买。如果我从列表中选择pune,则不起作用。我想要浦那或纳西克市也被选中。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您需要的是用多选下拉列表替换SECOND下拉列表。 在这里你可以找到很好的例子: http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/

祝你好运!

答案 1 :(得分:0)

您需要做的小改动请修改html 从

<select name="City" id="city" onChange="setcities();">
 <option value="">Please select a City </option>
</select>

<select name="City" id="city"">
 <option value="">Please select a City </option>
</select>

答案 2 :(得分:0)

class connect {

    public function __construct() {
        try {
            /    estabilish connection with the db credentials

            if (!$conn) {
                //    display error
            }
            return $conn;
        } catch (Exception $e) {
            //    throw exception
            $e->getMessage();
        }
    }

}
?>
<?php
include_once("conf.php");
include_once("function.php");
$object_function = new functions;
?>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<div>
    <select  name="select_data" id="select_state">
        <option>Please select</option>
        <?php
        $country_details = $object_function->state_details();
        $i = 0;
        while ($i < count($country_details)) {
            ?>
            <option value="<?php echo $country_details[$i]['STATEID'] ?>"><?php echo $country_details[$i]['STATENAME']; ?></option>
            <?php
            $i++;
        }
        ?>
    </select>
</div>
<div>
    <select  name="select_data" id="select_city">
        <option>Please select</option>

    </select>
</div>
<?php
error_reporting(E_ALL);
include_once("conf.php");
class functions extends connect {
    private $conn = '';
    public function __construct() {
        $this->conn = parent::__construct();
    }
    public function state_details() {
        try {
            $all_states = "select * from LWSTATEMASTER";
            $state_connection = oci_parse($this->conn, $all_states);
            $execute_query = oci_execute($state_connection);
            while ($state_row = oci_fetch_assoc($state_connection)) {
                $state_rows[] = $state_row;
            }
            return $state_rows;
        } catch (Exception $e) {
            $e->getMessage();
        }
    }
    public function city_details($country_data) {
        try {
            global $conn;
            $rows = array();
            $state_value = $country_data['state_value'];
            $all_cities = "select CITYID,CITYNAME from LWCITYMASTER where STATEID ='$state_value'";
            $city_connection = oci_parse($this->conn, $all_cities);
            $execute_query = oci_execute($city_connection);

            while ($city_row = oci_fetch_assoc($city_connection)) {
                $city_rows[] = $city_row;
            }
            $this->fetch_city_eachrows($city_rows);
        } catch (Exception$e) {
            $e->getMessage();
        }
    }

    public function fetch_city_eachrows($city_rows) {
        try {
            $i = 0;
            while ($i < count($city_rows)) {
                echo "<option value= " . $rows[$i]['CITYID'] . ">" . $city_rows[$i]['CITYNAME'] . "</option>";
                $i++;
            }
        } catch (Exception $e) {
            $e->getMessage();
        }
    }

}

$obj = new functions;
if (isset($_REQUEST['type']) == 'country') {
    $country_data = $_REQUEST;
    $obj->city_details($country_data);
}
?>
<script>
    $(document).ready(function () {
        $("#select_state").change(function () {
            var val = $('#select_state').val();
            $.ajax({
                type: "post",
                url: 'function.php',
                data: {'state_value': val, 'type': 'country'},
              `enter code here`  success: function (response) {
                    $("#select_city").html(response);
                    $("#select_city").show();
                }
            });
        });
    });
</script>