日期:2014-05-18  浏览次数:20484 次

给JS联动设置初始选项
C# code
 
<td>
<select id="sel_b" runat="server"> </select> <input id="hd_b" type="hidden" runat="server" />
<select id="sel_s" runat="server"> </select> <input id="hd_s" type="hidden" runat="server" />
<script type="text/javascript">
var arr_b = new Array("单位概况","政策法规","信息公开","公告栏","工作动态");
var arr_s = new Array(new Array("单位介绍", "大事记"), new Array("最新文件","国家法律","地方法规"),new Array("部门职责","部门职能","办事指南"),new Array("公告栏"),new Array("工作动态"));
function bind_b()
{

var sel = document.getElementById("sel_b");
sel.length = 0;
for(var i=0; i <arr_b.length; i++)
sel.options[i] = new Option(arr_b[i], arr_b[i]);
bind_s();
document.getElementById("ctl00_ContentPlaceHolder1_hd_b").value = arr_b[0];
sel.onchange = function(){ bind_s(); document.getElementById("ctl00_ContentPlaceHolder1_hd_b").value = sel.value;};
}
function bind_s()
{
var arr = arr_s[document.getElementById("sel_b").selectedIndex];
var sel = document.getElementById("sel_s");
sel.length = 0;
for(var i=0; i <arr.length; i++)
sel.options[i] = new Option(arr[i], arr[i]);
document.getElementById("ctl00_ContentPlaceHolder1_hd_s").value = arr[0];
sel.onchange = function(){ document.getElementById("ctl00_ContentPlaceHolder1_hd_s").value = sel.value;};
}
bind_b();
</script>

  JS代码就是这样 ,怎样在后台给这两个下拉设置初始值,值我已经从数据库提取了,应该怎样设置它们的初始值,跳转到这个页面后就按照查找出来的值显示

------解决方案--------------------
很简单以下是我帮你加了一条语句的代码:
<script type="text/javascript"> 
var arr_b = new Array("单位概况","政策法规","信息公开","公告栏","工作动态"); 
var arr_s = new Array(new Array("单位介绍", "大事记"), new Array("最新文件","国家法律","地方法规"),new Array("部门职责","部门职能","办事指南"),new Array("公告栏"),new Array("工作动态")); 
function bind_b() 


var sel = document.getElementById("sel_b"); 
sel.length = 0; 
for(var i=0; i <arr_b.length; i++) 
sel.options[i] = new Option(arr_b[i], arr_b[i]);
 sel.selectedIndex=1 //这是我给你加的代码默认值
bind_s(); 
document.getElementById("ctl00_ContentPlaceHolder1_hd_b").value = arr_b[0]; 

sel.onchange = function(){ bind_s(); document.getElementById("ctl00_ContentPlaceHolder1_hd_b").value = sel.value;}; 

function bind_s() 

var arr = arr_s[document.getElementById("sel_b").selectedIndex]; 
var sel = document.getElementById("sel_s"); 
sel.length = 0; 
for(var i=0; i <arr.length; i++) 
sel.options[i] = new Option(arr[i], arr[i]); 
document.getElementById("ctl00_ContentPlaceHolder1_hd_s").value = arr[0]; 
sel.onchange = function(){ document.getElementById("ctl00_ContentPlaceHolder1_hd_s").value = sel.value;}; 

bind_b(); 
</script>
------解决方案--------------------
先从后台加载事件里把你从数据库里取出来的数据分别赋值给两个隐藏框,

然后在判断那个值在下拉框的那个索引上,然后在把这个下拉框的索引指向它就行了,下面那个同理,

两联菜单,其实都不用写了,你都写好了,呵呵