一個簡單的jquery的多選下拉框(自寫)
更新時間:2014年05月05日 10:41:53 作者:
想在網(wǎng)上找一個插件用用,可是,網(wǎng)上的插件看起來都比較雜亂,我參考了網(wǎng)上的一些插件,自己用jquery寫了一個多選下拉框
今天做的項目要用到多選的下拉框,開始想在網(wǎng)上找一個插件用用,可是,網(wǎng)上的插件看起來都比較雜亂,我參考了網(wǎng)上的一些插件,自己用jquery寫了一個多選下拉框,js寫得比較簡潔。代碼如下。
js代碼
(function(){
$.fn.extend({
checks_select: function(options){
jq_checks_select = null;
$(this).click(function(e){
jq_check = $(this);
//jq_check.attr("class", "");
if (jq_checks_select == null) {
jq_checks_select = $("<div class='checks_div_select'></div>").insertAfter(jq_check);
$.each(options, function(i, n){
check_div=$("<div><input type='checkbox' value='" + n + "'>" + n + "</div>").appendTo(jq_checks_select);
check_box=check_div.children();
check_box.click(function(e){
//jq_check.attr("value",$(this).attr("value") );
temp="";
$("input:checked").each(function(i){
if(i==0){
temp=$(this).attr("value");
}else{
temp+="、"+$(this).attr("value");
}
});
jq_check.attr("value",temp);
e.stopPropagation();
});
});
}else{
jq_checks_select.toggle();
}
e.stopPropagation();
});
$(document).click(function () {
jq_checks_select.hide();
});
//$(this).blur(function(){
//jq_checks_select.css("visibility","hidden");
//alert();
//});
}
})
})(jQuery);
html
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js">
</script>
<script type="text/javascript" src="js/jquery2.1.js">
</script>
<script type="text/javascript" src="js/jquery-mah-UI.js">
</script>
<script language="javascript">
$(document).ready(function(){
var options = {
1: "第一個選擇項",
2: "第二個選擇項",
3: "第三個選擇項",
4: "第四個選擇項",
5: "第五個選擇項",
6: "第六個選擇項"
};
$("#test_div").checks_select(options);
});
</script>
<style>
.checks_div_select {
width: 150px;
background-color: #e9fbfb;
border: 1px solid #18cbcd;
font-family: 'Verdana', '宋體';
font-size: 12px;
position:absolute;
left:2px;
top:25px;
}
</style>
</head>
<body>
<div style="position:relative;">
<input type="text" id="test_div"/>
</div>
</body>
</html>
需要jquery類庫
js代碼
復制代碼 代碼如下:
(function(){
$.fn.extend({
checks_select: function(options){
jq_checks_select = null;
$(this).click(function(e){
jq_check = $(this);
//jq_check.attr("class", "");
if (jq_checks_select == null) {
jq_checks_select = $("<div class='checks_div_select'></div>").insertAfter(jq_check);
$.each(options, function(i, n){
check_div=$("<div><input type='checkbox' value='" + n + "'>" + n + "</div>").appendTo(jq_checks_select);
check_box=check_div.children();
check_box.click(function(e){
//jq_check.attr("value",$(this).attr("value") );
temp="";
$("input:checked").each(function(i){
if(i==0){
temp=$(this).attr("value");
}else{
temp+="、"+$(this).attr("value");
}
});
jq_check.attr("value",temp);
e.stopPropagation();
});
});
}else{
jq_checks_select.toggle();
}
e.stopPropagation();
});
$(document).click(function () {
jq_checks_select.hide();
});
//$(this).blur(function(){
//jq_checks_select.css("visibility","hidden");
//alert();
//});
}
})
})(jQuery);
html
復制代碼 代碼如下:
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js">
</script>
<script type="text/javascript" src="js/jquery2.1.js">
</script>
<script type="text/javascript" src="js/jquery-mah-UI.js">
</script>
<script language="javascript">
$(document).ready(function(){
var options = {
1: "第一個選擇項",
2: "第二個選擇項",
3: "第三個選擇項",
4: "第四個選擇項",
5: "第五個選擇項",
6: "第六個選擇項"
};
$("#test_div").checks_select(options);
});
</script>
<style>
.checks_div_select {
width: 150px;
background-color: #e9fbfb;
border: 1px solid #18cbcd;
font-family: 'Verdana', '宋體';
font-size: 12px;
position:absolute;
left:2px;
top:25px;
}
</style>
</head>
<body>
<div style="position:relative;">
<input type="text" id="test_div"/>
</div>
</body>
</html>
需要jquery類庫
相關文章
基于jQuery實現(xiàn)左右div自適應高度完全相同的代碼
最近做前端設計時需要使左右兩個DIV高度自適應。這其中的jquery代碼中的獲得高度用的clientHeight,介紹一下幾種不同的獲得方式以及他們的差別2012-08-08jQuery實現(xiàn)form表單基于ajax無刷新提交方法詳解
這篇文章主要介紹了jQuery實現(xiàn)form表單基于ajax無刷新提交方法,結合實例形式分析了jQuery使用$.ajax()方法實現(xiàn)無刷新提交的詳細步驟與實現(xiàn)技巧,并附帶了一個較為完整的實例供大家參考,需要的朋友可以參考下2015-12-12jQuery扁平化風格下拉框美化插件FancySelect使用指南
這篇文章主要介紹了jQuery扁平化風格下拉框美化插件FancySelect使用指南,需要的朋友可以參考下2015-02-02jQuery中attr()與prop()函數(shù)用法實例詳解(附用法區(qū)別)
這篇文章主要介紹了jQuery中attr()與prop()函數(shù)用法,結合實例形式詳細分析了attr()與prop()函數(shù)的使用技巧與相關注意事項,并附帶了attr()與prop()函數(shù)用法的區(qū)別,需要的朋友可以參考下2015-12-12DataTables+BootStrap組合使用Ajax來獲取數(shù)據(jù)并且動態(tài)加載dom的方法(排序,過濾,分頁等)
Datatables是一款jquery表格插件。它是一個高度靈活的工具,可以將任何HTML表格添加高級的交互功能。這篇文章主要介紹了DataTables+BootStrap組合使用Ajax來獲取數(shù)據(jù)并且動態(tài)加載dom的方法(排序,過濾,分頁等),需要的朋友可以參考下2016-11-11EasyUI的treegrid組件動態(tài)加載數(shù)據(jù)問題的解決辦法
最近涉及到treegrid組件的查詢,需要根據(jù)查詢條件動態(tài)更新EasyUI的treegrid組件的動態(tài)加載查詢結果2011-12-12