DEDECMS+Js实现自定义二级联动
DEDE有现成的二级联动管理,可以添加并生成二级联动的JS代码,使用起来非常方便。
如果要实现自己的效果,可能要对联动过程做一点开发。
下面的代码实例是在二级联动的过程中,添加了一个新的字段,以保存选项的值,并在前台根据JS的值做不同的事件。
<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
//查询根节点
$dsql->SetQuery("SELECT evalue,ename from `ddmx_sys_enum` where egroup='center' AND evalue%500 = 0 order by disorder asc, evalue asc");
$dsql->Execute();
$enums = array();
$sort = 1 ;
while($row = $dsql->GetArray())
{
$options .= "<option value=\"{$sort}\">{$row['ename']}</option>\r\n";
$enums[] = $row['evalue'];
$sort ++ ;
}
$count = count($enums);
foreach($enums as $i=>$evalue)
{
//添加了一个名为link的字段,保存选项的跳转链接
$dsql->SetQuery("SELECT evalue,ename,link from `ddmx_sys_enum` where egroup='center' AND evalue > {$evalue} AND evalue < ({$evalue} + 500) order by disorder asc, evalue asc");
$dsql->Execute();
$sort = 0 ;
$i++;
while($row = $dsql->GetArray())
{
$js .= "select2[". $i ."][{$sort}] = new Option(\"{$row['ename']}\", \"{$row['link']}\");\r\n";
$sort++;
}
}
$html = '
<form name="frm">
<div class="city_sch">
<h2 class="f13">选择里您最近的学习中心</h2>
<table width="190" height="128" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="51" align="center" valign="middle"><label>
<select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)">
<option selected>点击选择城市</option>
'. $options .'
</select>
</label></td>
</tr>
<tr>
<td height="22" align="center" valign="middle"><label>
<select name="s2" id="s2">
<option value="点击选择中心" selected>点击选择中心</option>
</select>
</label></td>
</tr>
<tr>
<td height="55" align="center" valign="middle"><label>
<input type="submit" name="button" id="button" value="马上提交" onclick="window.location=document.frm.s2.value;return false;"/>
</label></td>
</tr>
</table>
</div>
</form>
<script language="javascript">
var select1_len = document.frm.s1.options.length;
var select2 = new Array(select1_len);
for (i=0; i<select1_len; i++)
{
select2[i] = new Array();
}
select2[0][0] = new Option("请选择", " ");
'. $js .'
function redirec(x)
{
var temp = document.frm.s2;
for (i=0;i<select2[x].length;i++)
{
temp.options[i]=new Option(select2[x][i].text,select2[x][i].value);
}
temp.options[0].selected=true;
}
</script>
';
$html = addslashes(str_replace(array("\r\n", '"'), array('', '\''), $html));
echo "document.write(\" $html \")";
?>
本文来源:4wei
如果要实现自己的效果,可能要对联动过程做一点开发。
下面的代码实例是在二级联动的过程中,添加了一个新的字段,以保存选项的值,并在前台根据JS的值做不同的事件。
<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
//查询根节点
$dsql->SetQuery("SELECT evalue,ename from `ddmx_sys_enum` where egroup='center' AND evalue%500 = 0 order by disorder asc, evalue asc");
$dsql->Execute();
$enums = array();
$sort = 1 ;
while($row = $dsql->GetArray())
{
$options .= "<option value=\"{$sort}\">{$row['ename']}</option>\r\n";
$enums[] = $row['evalue'];
$sort ++ ;
}
$count = count($enums);
foreach($enums as $i=>$evalue)
{
//添加了一个名为link的字段,保存选项的跳转链接
$dsql->SetQuery("SELECT evalue,ename,link from `ddmx_sys_enum` where egroup='center' AND evalue > {$evalue} AND evalue < ({$evalue} + 500) order by disorder asc, evalue asc");
$dsql->Execute();
$sort = 0 ;
$i++;
while($row = $dsql->GetArray())
{
$js .= "select2[". $i ."][{$sort}] = new Option(\"{$row['ename']}\", \"{$row['link']}\");\r\n";
$sort++;
}
}
$html = '
<form name="frm">
<div class="city_sch">
<h2 class="f13">选择里您最近的学习中心</h2>
<table width="190" height="128" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="51" align="center" valign="middle"><label>
<select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)">
<option selected>点击选择城市</option>
'. $options .'
</select>
</label></td>
</tr>
<tr>
<td height="22" align="center" valign="middle"><label>
<select name="s2" id="s2">
<option value="点击选择中心" selected>点击选择中心</option>
</select>
</label></td>
</tr>
<tr>
<td height="55" align="center" valign="middle"><label>
<input type="submit" name="button" id="button" value="马上提交" onclick="window.location=document.frm.s2.value;return false;"/>
</label></td>
</tr>
</table>
</div>
</form>
<script language="javascript">
var select1_len = document.frm.s1.options.length;
var select2 = new Array(select1_len);
for (i=0; i<select1_len; i++)
{
select2[i] = new Array();
}
select2[0][0] = new Option("请选择", " ");
'. $js .'
function redirec(x)
{
var temp = document.frm.s2;
for (i=0;i<select2[x].length;i++)
{
temp.options[i]=new Option(select2[x][i].text,select2[x][i].value);
}
temp.options[0].selected=true;
}
</script>
';
$html = addslashes(str_replace(array("\r\n", '"'), array('', '\''), $html));
echo "document.write(\" $html \")";
?>
本文来源:4wei
顶(1)
踩(0)
- 最新评论