$("select.cmdtype-list").change(function(){
            var data = {id: $('select.cmdtype-list option:selected').val()};
            alert(getRootPath() + '/task/getcommand');
            $.getJSON(getRootPath() + '/task/getcommand', data,  function(json){
                var vx = json.text;
                $(".cmd_prj").empty();
                $.each(vx,function(i,item){
                    $(".cmd_prj").append(item);
                });                
            });
       })其中 json为getJSON 返回的数组用于填充 第二个combox , 但是填充出来为空。不知语法有何问题?能这样添吗?JavaScriptjquery

解决方案 »

  1.   

    不一定是填充出来为空,也有可能是后端根本没返回数据,贴出你alert(json)的结果
    以及你后端返回JSON的格式
      

  2.   

    alert(json)的结果     后端 :
                
                $json = array();         
                $json['text'] = array("1","2","3");;
                
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
                header('Cache-Control: no-cache, must-revalidate, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
                header('Pragma: no-cache');
                header('Content-Type: text/json');            echo CJSON::encode($json);
      

  3.   


    //你的代码改成
     var vx = json.text;   
     $(".cmd_prj option").remove();
     var str=''; 
     $.each(vx,function(i,item){      
    str=str+"<option>"+item+"</option>";        
     });                
     $("select").append(str);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){//我的测试用例
    var a=[1,2,3];
    $("#abv").click(function(){
    $("select option").remove();
    var str='';
    $.each(a,function(i,j){
    str=str+"<option>"+j+"</option>";
    });
    $("select").append(str);
    });
    })
    </script>
    <input type="button" id="abv" value="测试用按钮"/> 
    <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    </select>