Jquery ajax动态更新下拉列表的内容

     $("#book_id").change(function(){
          $book_id=$(this).children('option:selected').val();
          //alert($book_id);
          var optionstring="";
         $.ajax(
                    {
                        type : "post", 
                        url : "__CONTROLLER__/getpersonbybook", 
                        data: {book_id:$book_id},
                        success : function (result)
                        {
                            if(result.status==1){
                                $("#person_id").empty(); 
                                $("#secperson_id").empty();
                                $.each(result.persons,function(key,value){  //循环遍历后台传过来的json数据  
                                     optionstring += "<option value=\"" + value.id + "\" >" + value.person_name + "</option>";  
                                 });  
                                 $("#person_id").html("<option value=''>请选择人物</option> "+optionstring); 
                                 $("#secperson_id").html("<option value=''>请选择人物</option> "+optionstring); 
                            }
                            //给下拉赋值
                        }
                    });
        });

后台使用thinkphp返回json:

    public  function getpersonbybook($book_id){
        if($book_id==""){
            $list["status"]="0";
            $list["message"]="请选择书籍";
        }
        else{
            $list["status"]="1";
            $list["message"]="返回人物成功";
            $persons=M('Person')->where("person_del=0 and person_ofbook=$book_id")->order('id asc')->select();
            $list["persons"]=$persons;
        }
        $this->ajaxReturn($list);
    }

 jquery选中特定的text:

     //定位关系开始
          $("#btnPosirelation").click(function(){ 
         var p=$('#p3_text').val();
         if(p==""){
             return;
         }
        var count=$("#relation_id option").length;
          for(var i=0;i<count;i++)  
             {           
              if($("#relation_id ").get(0).options[i].text == p)  {  
                    $("#relation_id ").get(0).options[i].selected = true;  
                    break;  
                }  
            }
     });
     //定位关系结束

 

posted @ 2017-06-26 14:51  幸福安康  阅读(2530)  评论(0编辑  收藏  举报