通過隱藏option實現(xiàn)select的聯(lián)動效果
更新時間:2009年11月10日 23:46:45 作者:
開始的時候需求是根據一定條件隱藏一部分<option>標簽,類似聯(lián)動效果,但是目前的html規(guī)范并沒有為<option>提供隱藏的效果,因此常用的設置display或者visibility無效。
復制代碼 代碼如下:
<!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>
這樣,通過上一個下拉框的選擇過濾下拉框的內容,基本實現(xiàn)了隱藏<option>的效果,當然,也可以把這種方法利用在下拉框級聯(lián)選擇的功能上,無需Ajax。
該代碼在IE6,IE7,Chrome2,F(xiàn)irefox3。5下驗證通過。
您可能感興趣的文章:
- 實現(xiàn)一個年、月、季度聯(lián)動SELECT的javascript代碼
- jquery select操作的日期聯(lián)動實現(xiàn)代碼
- lyhucSelect基于Jquery的Select數(shù)據聯(lián)動插件
- js操縱跨frame的三級聯(lián)動select下拉選項實例介紹
- 簡單實用jquery版三級聯(lián)動select示例
- js簡單實現(xiàn)HTML標簽Select聯(lián)動帶跳轉
- 兩級聯(lián)動select刷新后其值保持不變的實現(xiàn)方法
- Select標簽下拉列表二級聯(lián)動級聯(lián)實例代碼
- jQuery select表單提交省市區(qū)城市三級聯(lián)動核心代碼
- jQuery制作簡潔的多級聯(lián)動Select下拉框
- javascript實現(xiàn)無限級select聯(lián)動菜單
相關文章
input、button的不同type值在ajax提交表單時導致的陷阱
最近在公司的項目中嘗試著利用YUI來實現(xiàn)各種效果。2009-02-02FileUpload 控件 禁止手動輸入或粘貼的實現(xiàn)代碼
FileUpload 控件 禁止手動輸入或粘貼的實現(xiàn)代碼,經常用于比較安全無誤的輸入,例如身份證號碼,密碼,銀行賬戶等。2010-04-04javascript中input中readonly和disabled區(qū)別介紹
javascript中input中readonly和disabled區(qū)別,在開發(fā)中可能會常有用到,利用js動態(tài)改變input的屬性2012-10-10