A simple implementation of the drop down box multiple selected plug in portability is better

  • 2020-03-30 02:48:11
  • OfStack

In the use of the last written a multi-first drop-down box, I found a lot of problems, after modification and improvement, now can be used well, and portability is also relatively good, the following is the source code.

js
 
(function(){ 

$.fn.extend({ 
checks_select: function(options){ 
jq_checks_select = null; 
$(this).val("--- Please select a ---"); 
$(this).next().empty(); //Clears out
$(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("--- Please select a ---"); 
} 
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: " The first option ", 
2: " Second option ", 
3: " The third option ", 
4: " The fourth option ", 
5: " The fifth option ", 
6: " The sixth option " 
}; 
$("#test_div").checks_select(options); 
}); 
</script> 
<style> 
.checks_div_select { 
width: 150px; 
background-color: #e9fbfb; 
border: 1px solid #18cbcd; 
font-family: 'Verdana', ' Song typeface '; 
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> 

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/201405050943571.gif? 20144594422 ">

Related articles: