Use CSS and jQuery to simulate a select with code that gets the data after it's committed

  • 2020-03-26 21:27:52
  • OfStack

Simulates a select with code that fetches data after a commit
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201310/20131018170537.gif? 20139181769 ">  
The HTML Code
 
<div id="dropdown"> 
<p> Please select city </p> 
<ul> 
<li><a href="#" rel="2"> Beijing </a></li> 
<li><a href="#" rel="3"> Shanghai </a></li> 
<li><a href="#" rel="4"> wuhan </a></li> 
<li><a href="#" rel="5"> Guangzhou </a></li> 
</ul> 
</div> 
<div id="result"></div> 

JavaScript Code
 
<script type="text/javascript"> 
$(function(){ 
$("#dropdown p").click(function(){ 
var ul = $("#dropdown ul"); 
if(ul.css("display")=="none"){ 
ul.slideDown("fast"); 
}else{ 
ul.slideUp("fast"); 
} 
}); 
$("#dropdown ul li a").click(function(){ 
var txt = $(this).text(); 
$("#dropdown p").html(txt); 
var value = $(this).attr("rel"); 
$("#dropdown ul").hide(); 
$("#result").html(" You select the "+txt+" The value is: "+value); 
}); 

}); 
</script> 

The CSS Code
 
#dropdown{width:186px; margin:100px auto; position:relative} 
#dropdown p{width:150px; height:24px; line-height:24px; padding-left:4px; padding-right:30px; border:1px solid #a9c9e2; background:#e8f5fe url(arrow.gif) no-repeat rightright 4px; color:#807a62; cursor:pointer} 
#dropdown ul{width:184px; background:#e8f5fe; margin-top:2px; border:1px solid #a9c9e2; position:absolute; display:none} 
#dropdown ul li{height:24px; line-height:24px; text-indent:10px} 
#dropdown ul li a{display:block; height:24px; color:#807a62; text-decoration:none} 
#dropdown ul li a:hover{background:#c6dbfc; color:#369} 
#result{margin-top:10px;text-align:center} 

Related articles: