0

我想填充另一个下拉列表的下拉列表,例如国家更改,然后状态 dorpdown 应该填充并更改州城市。我正在尝试通过 Ajax 执行此操作,但它没有响应。我写的代码是

<script type="text/javascript">
    $(document).ready(function(){
        load_options('','country');
    });

    function load_options(id,index){
        $("#loading").show();

        if(index=="state"){
            $("#city").html('<option value="">Select city</option>');
        }

        $.ajax({
            url: "/ajax.php?index="+index+"&id="+id,
            complete: function(){$("#loading").hide();},
            success: function(data) {
                $("#"+index).html(data);
            }
        })
    }
</script>

HTML:

<label>Select Country</label>
<select name="country" id="country" onChange="load_options(this.value,'state');">
    <option value="">Select country</option>
</select>

<label>Select State</label>
<select name="state" id="state" onChange="load_options(this.value,'city');">
    <option value="">Select state</option>
</select>

<label>Select city</label>
<select name="city" id="city">
    <option value="">Select City</option>
</select>

阿贾克斯.php:

<?php
    class AJAX {
        private $database = NULL;
        private $_query = NULL;
        private $_fields = array();
        public  $_index = NULL;
        const DB_HOST = "localhost";
        const DB_USER = "root";
        const DB_PASSWORD = "12345";
        const DB_NAME = "AnandDb";

        public function __construct(){
            $this->db_connect();                 // Initiate Database connection
            $this->process_data();
        }
        /*
         *  Connect to database
        */

        private function db_connect(){
            $this->database = mysql_connect(self::DB_HOST,self::DB_USER,self::DB_PASSWORD);

            if($this->database){
                $db =  mysql_select_db(self::DB_NAME,$this->database);
                echo "Db connect.";
            } else {
                echo mysql_error();die;
                echo "Db Not connected.";
            }
        }

        private function process_data(){
            $this->_index = ($_REQUEST['index'])?$_REQUEST['index']:NULL;
            $id = ($_REQUEST['id'])?$_REQUEST['id']:NULL;
            switch($this->_index){
                case 'country':
                $this->_query = "SELECT country_id,country_name FROM tbl_country";
                $this->_fields = array('id','country_name');
                break;

                case 'state':
                $this->_query = "SELECT * FROM tbl_state WHERE country_id=$id";
                $this->_fields = array('id','state_name');
                break;

                case 'city':
                $this->_query = "SELECT * FROM tbl_city WHERE state_id=$id";
                $this->_fields = array('id','city_name');
                break;

                default:
                break;
            }
            $this->show_result();
        }

        public function show_result(){
            echo '<option value="">Select '.$this->_index.'</option>';
            $query = mysql_query($this->_query);
            while($result = mysql_fetch_array($query)){
                $entity_id = $result[$this->_fields[0]];
                $enity_name = $result[$this->_fields[1]];
                echo "<option value=".$entity_id.">".$enity_name."</option>";
            }
        }
    }
    $obj = new AJAX;
 ?>
4

2 回答 2

0

我觉得没有任何其他错误。但不要在 javascript 函数中使用 id 和 index。重命名这些变量并尝试。试着提醒那两个。

于 2013-04-12T12:22:38.703 回答
0

尝试这个:

<select name="country" id="country" 
onChange="load_options(this.options[this.selectedIndex].value,'state');">
于 2013-04-12T12:28:38.693 回答