附加<;选项>;至<;选择>;使用AJAX&;PHP

附加<;选项>;至<;选择>;使用AJAX&;PHP,php,jquery,ajax,database,Php,Jquery,Ajax,Database,我的数据库中有两个表: 1.动物类型 2.品种 动物类型: type_id | name ------------------------ 1 | Dog 2 | Cat 3 | etc... $(document).ready(function() { // $("#breeds").attr('disabled', true); // check if selected if($("#animal_type"

我的数据库中有两个表:

1.动物类型 2.品种

动物类型:

type_id  |    name
------------------------
1        |  Dog
2        |  Cat
3        |  etc...
$(document).ready(function() {

   // $("#breeds").attr('disabled', true);

    // check if selected
    if($("#animal_type").find('option:selected').val() == 0) {
        $("#breeds").attr('disabled', true);
    }

    $('#animal_type').change(function(){
        // get value of selected animal type
        var selected_animal_type = $(this).find('option:selected').val();

        $.ajax({
            url : "url-to-site/index.php/account/getBreedsByTypeID/1",
            type:'POST',
            dataType: 'json',
            success: function(response) {
                $("#breeds").attr('disabled', false);

                //alert(response); // show [object, Object]

                var $select = $('#breeds');

                $select.find('option').remove();
                $.each(response,function(key, value)
                {
                    $select.append('<option value=' + key + '>' + value + '</option>'); // return empty
                });
             }
        });
    });
 });
品种

ID   |  type_id |  name 
---------------------------
1    |    1     | Labrador
2    |    1     | Putbull
3    |    2     | Persian
etc.....
在我的首页,我想显示选择下拉菜单与动物类型和品种选择基于动物类型

1。选择动物类型

<select id="animal_type">
   <option value="<?php $type->id">Dog</option>
   <option value="<?php $type->id">Cat</option>
</select>
<!-- user select Dog type!  fetch all dog breeds -->
<select id="breeds" disabled>
   <option value="<?php $type->id">Labrador</option>
   <option value="<?php $type->id">Pitbull</option>
</select>
此url返回以下编码的JSON

$breeds = $this->animal_breed->findAllBreedsByType($id); // Model @return array

return json_encode($breeds);
那么,如何将该结果附加到基于类型的选择中呢

你能举个例子来解决这个问题吗?谢谢。

$.ajax({
$.ajax({
            url : "url-to-site/index.php/account/getBreedsByTypeID/1",
            type:'POST',
            dataType: 'json',
            success: function(response) {
                $("#breeds").attr('disabled', false);
                $.each(response,function(key, value)
                {
                    $("#breeds").append('<option value=' + key + '>' + value + '</option>');
                });
             }
        });
url:“指向site/index.php/account/getBreedsByTypeID/1的url”, 类型:'POST', 数据类型:“json”, 成功:功能(响应){ $(“#品种”).attr('disabled',false); $。每个(响应、功能(键、值) { $(“#品种”)。追加(“”+value+“”); }); } });
$.ajax({
url:“指向site/index.php/account/getBreedsByTypeID/1的url”,
数据:{id:selected_animal_type},
类型:'POST',
数据类型:“json”,
成功:功能(响应){
$(“#品种”).attr('disabled',false);
$。每个(响应、功能(键、值)
{
$(“#品种”).append(“”+value.bride_name+“”);
});
}
});

您的
$中缺少
数据
参数。ajax
请求,请尝试添加
数据:{id:selected\u animal\u type}
。另外,您确定URL是正确的吗?
$.ajax({
            url : "url-to-site/index.php/account/getBreedsByTypeID/1",
            type:'POST',
            dataType: 'json',
            success: function(response) {
                $("#breeds").attr('disabled', false);
                $.each(response,function(key, value)
                {
                    $("#breeds").append('<option value=' + key + '>' + value + '</option>');
                });
             }
        });
$.ajax({
        url : "url-to-site/index.php/account/getBreedsByTypeID/1",
      data:{id:selected_animal_type},             
        type:'POST',
        dataType: 'json',
        success: function(response) {
            $("#breeds").attr('disabled', false);
            $.each(response,function(key, value)
            {
                $("#breeds").append('<option value=' + key + '>' + value.breed_name + '</option>');
            });
         }
    });