通过隐藏option实现select的联动效果

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(function(){
//Bind the change event
$("#dropLang").unbind("change", eDropLangChange).bind("change", eDropLangChange);
$("#dropFrame").unbind("change", eDropFrameChange).bind("change", eDropFrameChange);
});

//The change event of language dropdown-list
var eDropLangChange = function(){
//The selected value of the language dropdown-list.
var selectedValue = $(this).val();

//show all options.
$("#dropFrame").children("span").each(function(){
$(this).children().clone().replaceAll($(this)); //use the content of the <span> replace the <span>
});

//Filter the data through selected value of language dropdown-list except <Please Select>.
//If the option is <Please Select>, it only needs to show all and hide nothing.
if(parseInt(selectedValue) != 0){
//hide the option whose parentid is not equal with selected value of language dropdown-list.
//The <Please Select> option should not be hidden.
$("#dropFrame").children("option[parentid!='" + selectedValue + "'][value!='0']").each(function(){
$(this).wrap("<span style='display:none'></span>"); //add a <span> around the <option> and hide the <span>.
});
}
};

//The change event of frame dropdown-list.
var eDropFrameChange = function(){
//Find the selected option of frame dropdown-list. set the value of language dropdown-list by selected parentid.
$("#dropLang").val($(this).children("option:selected").attr("parentid"));
};
</script>
</head>
<body>
<div>
<select id="dropLang">
<option selected="selected" value="0"><Please Select></option>
<option value="1">Javascript</option>
<option value="2">Java</option>
<option value="3">C#</option>
</select>
<select id="dropFrame">
<option selected="selected" value="0"><Please Select></option>
<option value="1" parentid="1">JQuery</option>
<option value="2" parentid="1">Prototype</option>
<option value="3" parentid="2">Struts</option>
<option value="4" parentid="2">Spring</option>
<option value="5" parentid="2">Velocity</option>
<option value="6" parentid="2">Hibernate</option>
<option value="7" parentid="3">ASP.NET MVC</option>
<option value="8" parentid="3">Castle</option>
</select>
</div>
</body>
</html>

这样,通过上一个下拉框的选择过滤下拉框的内容,基本实现了隐藏<option>的效果,当然,也可以把这种方法利用在下拉框级联选择的功能上,无需Ajax。

该代码在IE6,IE7,Chrome2,Firefox3。5下验证通过。

(0)

相关推荐

  • javascript实现无限级select联动菜单

    代码很简单,这里主要是给大家推荐一下本代码的实现思路,别具一格. 奉上代码: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&g

  • lyhucSelect基于Jquery的Select数据联动插件

    数据源: 复制代码 代码如下: var areaInfo = new Array(); areaInfo[0] = new Array(); areaInfo[0][0]="1"; areaInfo[0][1]="Beijing"; areaInfo[0][2]="0"; areaInfo[0][3]="0"; areaInfo[1] = new Array(); areaInfo[1][0]="2"; a

  • Select标签下拉列表二级联动级联实例代码

    首先从服务器端,绑定下拉列表,二级下拉的text命名按照一定规则加上一级下拉的ID. 复制代码 代码如下: var options=new Array();    $(document).ready(function(){        //二级联动        $('#ddlPages').children('option').each(function(i){            options[i]='<option value="'+$(this).val()+'"&

  • jquery select操作的日期联动实现代码

    Jquery的选择器很强大,对select的options对象添加的时候我找了老半天才找到 复制代码 代码如下: /**//* 文件名:jquery.liu.select.js 功能说明:本js文件为jquery类库的一个插件,主要实现对select的操作. 作者:John Liu 编写日期:2008/03/12 */ //得到select项的个数 jQuery.fn.size = function() { return jQuery(this).get(0).options.length; }

  • 两级联动select刷新后其值保持不变的实现方法

    背景 还是学院和专业的关系,我需要保持点击提交按钮后,页面select中继续维持提交前的值 网上有几种办法,要么通过url跳转时候附带参数形式传回页面,或者提交时候讲select的值保存在cookie中,提交后再把cookie中的值填回select中. 我是按照保存在cookie中的方法实现刷新后select值不变,对select设置一个onchange事件 复制代码 代码如下: <script type="text/javascript"> window.onload =

  • jQuery select表单提交省市区城市三级联动核心代码

    jQuery select表单提交省市区城市三级联动,引用的是"jquery-1.7.min"类库,地区码查询地区名用数组存在AreaData_min,下面摘要部分代码: SelectArea.htm文件 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transition

  • js操纵跨frame的三级联动select下拉选项实例介绍

    运用HTML.CSS以及Javascript相关知识,编写多窗口多菜单的内容联动. 考察学生关于frame.浏览器对象,以及表单控件的概念,javascript事件触发机制和程序设计, 以及用CSS来组织文字展示的掌握程度,使学生对DHTML有更加深刻的理解. 实验内容[必做] (1)建立一个包含三个frame的窗口 (2)第一个frame1中包含一个select,内容是中国的各个省 (3)第二个frame2中同样含有一个select,内容是各省的地级市 (4)第三个frame3用来显示关于某地

  • jQuery制作简洁的多级联动Select下拉框

    今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这个Select下拉框也可以绑定下拉事件,并获取当前选中项的值. html代码: 复制代码 代码如下: <div class="wrap">         <div class="nice-select" name="nice-select&

  • 实现一个年、月、季度联动SELECT的javascript代码

    请选择报告类型 月度报告 季度报告 半年报告 年度报告 function GetValue(){ var objlist = document.getElementsByTagName("SELECT"); var tmp = new Array(); var tap = objlist[0].options[objlist[0].selectedIndex].text; strTmp=""; var j = 1; for(var i=1;i0){ select.o

  • 简单实用jquery版三级联动select示例

    html和js部分 复制代码 代码如下: <!DOCTYPE html> <html> <head> <meta charset=gbk /> <title>selectList</title> <style type="text/css"> *{margin:0;padding:0;} .selectList{width:200px;margin:50px auto;} </style>

  • js简单实现HTML标签Select联动带跳转

    复制代码 代码如下: <script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script> 复制代码 代码如下: <FORM name="form5" id="form5" action="" method="post" target="_blank" ons

随机推荐