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

隐藏Option标签技巧
本文介绍了一种不依赖Ajax实现<option>标签隐藏的方法,通过在外层包裹<span>标签并设置display:none来达到目的。适用于联动下拉菜单等场景。
开始的时候需求是根据一定条件隐藏一部分<option>标签,类似联动效果,但是目前的html规范并没有为<option>提供隐藏的效果,因此常用的设置display或者visibility无效。网上大部分解决方案是删除<option>节点或<option>置空。这显然不能够满足需求。后来经过试验,选择了利用标签包装的解决方案,基本原理如下:
  当<option>需要隐藏的时候,在<option>标签外包装一个<span>标签,再令<span>标签为不可见。
  当<option>需要显示的时候,恢复其正常的状态,即,去掉外面的<span>标签。
  由于比较懒,所以利用JQuery框架来操作DOM对象和CSS,代码如下:

<!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下验证通过。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26182553

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值