| fleaphp结合jquery和json实现联动菜单 |
| 2008-07-27 | |
|
之前在做联动菜单时,只是简单的利用ajax将服务端打印的select控件的html直接生成到当前页面,发现这样在ff下取不到select的值,后改用遍历json来生成option的办法,解决这一问题。代码如下:
js代码: function makeServerList () { if (document.form1.game_id.value != '') { delseloptions(); var gameid = document.form1.game_id.value; $.post('index.php?controller=default&action=getlist&r='+Math.random(),{gameid:gameid},function (json) { eval("d = "+json+";"); for (var i=0;i<d.lists.length;i++) { var val = d.lists[i].id+'|'+d.lists[i].name; AddOption(d.lists[i].name,val); } }); } } function AddOption(Text,Value) { var Text,Value,TemObj=document.form1.server_list; var TemOption=new Option(Text,Value); TemObj[TemObj.length]=TemOption; } function delseloptions() { var obj = document.getElementById('server_list');; obj.length = 1; } html代码: <tr> <td align="right">Select a TQ Game:</td> <td> <select name="game_id" id="game_id" valid="required" errmsg="Please select a game." onchange="return makeServerList();"> <option value="" selected="selected">Please Select</option> <option value="3">Conquer Online</option> <option value="2">Eudemons online</option> <option value="26">Zero online</option> <option value="24">Crazy Tao</option> </select> </td> </tr> <tr> <td align="right">Select a Server:</td> <td> <select name="server_list" id="server_list" valid="required" errmsg="Please select a server."> <option value="" selected="selected">Please Select</option> </select> </td> </tr> php代码: function actionGetlist() { $gameid = intval($_REQUEST['gameid']); $arr = require(CACHE_DIR.'serverList'.$gameid.'.php'); $new = array(); foreach ($arr as $val) { $temp['id'] = $val[0]; $temp['name'] = $val[1]; array_push($new,$temp); } $lists['lists'] = $new; require('./common/Json.class.php'); $json = new Json(); $str = $json->encode($lists); $str = str_replace('\n', '', $str); echo $str; } |
| 下一篇 > |
|---|