php代码
public function convert_array(){ $arr = array( '0'=>array('name'=>'zc','height'=>173), '1'=>array('name'=>'tom','height'=>175), '2'=>array('name'=>'jm','height'=>173), '3'=>array('name'=>'tang','height'=>173), ); echo json_encode($arr); //必须echo }
html代码
<input type="button" value='转换' id='convert' /> <div id="test"></div>
js代码
$("#convert").bind('click',function(){ $.getJSON("http://www.ci.com/select/convert_array", function(json){ var json = eval(json); //生成select对象 var select_object = $('<select>',{'id':'myselect','name':'myselect'}); for(var i=0;i<json.length;i++){ var option_object = $('<option>',{'value':json[i].height,'text':json[i].name}); select_object.append(option_object); } //将select 添加到div中 $('#test').append(select_object); }); });
生成结果如图所示: