Home > Web Front-end > JS Tutorial > Use jQuery to create a simple drop-down box example

Use jQuery to create a simple drop-down box example

怪我咯
Release: 2017-03-31 09:22:00
Original
1243 people have browsed it

Let’s take a look at the renderings first:


Use jQuery to create a simple drop-down box example

Everyone must have seen web pages with similar effects. How to achieve them? , the code is very simple:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
* { margin:0; padding:0; }
p.centent {
  float:left;
  text-align: center;
  margin: 10px;
}
span { 
 display:block; 
 margin:2px 2px;
 padding:4px 10px; 
 background:#898989;
 cursor:pointer;
 font-size:12px;
 color:white;
}
</style>
<!--  引入jQuery -->
<script src="jquery-2.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
 //移到右边
 $(&#39;#add&#39;).click(function() {
 //获取选中的选项,删除并追加给对方
 $(&#39;#select1 option:selected&#39;).appendTo(&#39;#select2&#39;);
 });
 //移到左边
 $(&#39;#remove&#39;).click(function() {
 $(&#39;#select2 option:selected&#39;).appendTo(&#39;#select1&#39;);
 });
 //全部移到右边
 $(&#39;#add_all&#39;).click(function() {
 //获取全部的选项,删除并追加给对方
 $(&#39;#select1 option&#39;).appendTo(&#39;#select2&#39;);
 });
 //全部移到左边
 $(&#39;#remove_all&#39;).click(function() {
 $(&#39;#select2 option&#39;).appendTo(&#39;#select1&#39;);
 });
 //双击选项
 $(&#39;#select1&#39;).dblclick(function(){ //绑定双击事件
 //获取全部的选项,删除并追加给对方
 $("option:selected",this).appendTo(&#39;#select2&#39;); //追加给对方
 });
 //双击选项
 $(&#39;#select2&#39;).dblclick(function(){
  $("option:selected",this).appendTo(&#39;#select1&#39;);
 });
});
</script>

</head>
<body>
 <p class="centent">
 <select multiple="multiple" id="select1" style="width:100px;height:160px;">
  <option value="1">曹操</option>
  <option value="2">曹昂</option>
  <option value="3">曹丕</option>
  <option value="4">曹彰</option>
  <option value="5">曹植</option>
  <option value="6">曹熊</option>
  <option value="7">曹仁</option>
  <option value="8">曹洪</option>
  <option value="9">曹休</option>
  <option value="10">曹真</option>
  <option value="11">曹爽</option>
 </select>
 <p>
  <span id="add" >选中添加到右边>></span>
  <span id="add_all" >全部添加到右边>></span>
 </p>
 </p>
 <p class="centent">
 <select multiple="multiple" id="select2" style="width: 100px;height:160px;">
  <option value="12">曹芳</option>
 </select>
 <p>
  <span id="remove"><<选中删除到左边</span>
  <span id="remove_all"><<全部删除到左边</span>
 </p>
 </p>
</body>
</html>
Copy after login

Functions implemented by the code:

1), add the selected options to the other party

2), add all options to The other party

3), double-click an option to add it to the other party

The above is the detailed content of Use jQuery to create a simple drop-down box example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template