Combobox与Jquery的动态区

时间:2014-06-30 02:32:16

标签: php jquery yii combobox

我是Jquery和Yii框架的新手。我对这个问题有疑问。

表:

有两个表

CITY: city_idname

DISTRICT: dis_id, city_id, name (city_id is foreign key of CITY(city_id))

当我同时选择城市名称时,任何人都可以帮助我创建2个组合框它显示了区域。 例如,任何代码?

提前致谢。

抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

尝试阅读如下,

//你的Jquery Ajax看起来应该是这样的

$("#firstcomboboxid").change(function() { //when your first combobox made changes
    $.post( "/controllername/functionname", $( "#yourform" ).serialize(), function(data){        
        //Response from server after query
        if(data.result == 'success'){
            $("#secondcomboboxid").empty(); //Make sure the combobox is empty
            $.each(data.district,function(i,val)){
                $("#secondcomboboxid").html("<option value='"+i+"'>"+val+"</option>"); //bind every of them into combobox
            }
        }
    },'json');
}

//你的控制器{controllername截至目前}

public function functionname(){ //functionname to be replace
    /* Starts your query here */

    $result = .....; //For example we will use $result as variable and assuming it will be an array as result.
    /* End of your query */

    exit(json_encode(array('result' => 'success','district' => $result))); //Return with JSON encode
}

希望这会有所帮助。感谢。