两个select框联动

select框联动

之前项目要用,所以找了个,自己改了一丢丢

html代码
<select name="type_id" id="type_id">
    
    {foreach $res as $value}
    
    {/foreach}
select>
<select name="material_id" id="material_id" >
    
select>
JQ代码
<script type="text/javascript">
    function getSelectVal(){ 
        var material_id = $("#material_id"); 
        $("option",material_id).remove(); 
         $.ajax({    
            type:"get",    
            url:"/mall/config_commodity_info/info",    
            data:{type_id:$("#type_id").val()},   
            success:function(select){ 
                  var obj =  JSON.parse(select); 
                  for (var key in obj) {
                        var option = "+obj[key]+""; 
                        material_id.append(option); 
                    }
            }  
    }); 
}
$(function(){ 
    getSelectVal(); 
    $("#type_id").change(function(){ 
        getSelectVal(); 

    }); 
}); 
PHP代码
public function info()
    {
        $type_id = input('type_id');
        $select = [];
        if(isset($type_id)){
            $res = $this->add_material->selTypeId($type_id); 
            foreach ($res as $value) {
                $select[$value['material_id']] = $value['material_name'];
            }           
        }

        echo json_encode($select); 
    }

你可能感兴趣的:(PHP,HTML,select框,PHP)