一個簡單的實現下拉框多選的外掛程式可移植性比較好

來源:互聯網
上載者:User

在使用上次寫的一個多先下拉框時,我發現了很多問題,經過修改和完善後,現在已經能夠很好地使用了,且可移植性也比較好,下面是原始碼。

js
複製代碼 代碼如下:
(function(){

$.fn.extend({
checks_select: function(options){
jq_checks_select = null;
$(this).val("---請選擇---");
$(this).next().empty(); //先清空
$(this).unbind("click");
$(this).click(function(e){
jq_check = $(this);
//jq_check.attr("class", "");
if (jq_checks_select == null) {
jq_checks_select = jq_check.next();
jq_checks_select.addClass("checks_div_select");
//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="";
$(this).parents().find("input:checked").each(function(i){
if(i==0){
temp=$(this).val();
}else{
temp+=","+$(this).val();
}
});
//alert(temp);
jq_check.val(temp);
e.stopPropagation();
});
});
jq_checks_select.show();
}else{
jq_checks_select.toggle();

}
e.stopPropagation();
});
$(document).click(function () {
flag=$("#test_div");
if(flag.val()==""){
flag.val("---請選擇---");
}
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.js">
</script>
<script type="text/javascript" src="js/jquery_mutili.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" readonly="readonly"/>
<div></div>
</div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.