jquery MultiSelect Plug-in implementation operation multi-selection list box

Source: Internet
Author: User
Tags html page

MultiSelect has the following functional features:

You can move (swap) items on both sides of the list box.

You can move an item in a list box by double-clicking the event.

You can set the maximum number of selected items.

You can customize the appearance style.

How to use it?

1, prepare jquery and jquery.multiselect2side plug-ins and CSS style.

The code is as follows Copy Code

<script type= "Text/javascript" src= "Js/jquery.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.multiselect2side.js" ></script>
<link rel= "stylesheet" type= "Text/css" href= "Jquery.multiselect2side.css"/>

Plug-ins and style files are already included in the demo and you can download the entire demo to use.

2, XHTML.

  code is as follows copy code

<select name=" lioption[] " id= ' lioption '  multiple= ' Multiple '  size= ' 8 '  > 
    <option value= "PHP" >PHP</ OPTION>&NBSP
    <option value= "MYSQL" >MYSQL</OPTION>&NBSP;
    <option value= "asp.net" >asp.net</option> 
     <option value= "XHTML" >xhtml</option> 
    <option  Value= "CSS" >css</option> 
    <option value= "JQUERY" >JQUERY </OPTION>&NBSP
</select> 

I assume that some static entries already exist in the list box, which can be loaded in dynamically. If you already have a selected item by default, set the property selected of option to "selected". Such as:

The code is as follows Copy Code

<option value= "PHP" selected= "selected" >PHP</option>

3, the application of MultiSelect Plug-ins.

  code is as follows copy code

$ (function ( { 
   $ ("#liOption"). Multiselect2side ({ 
         selectedPosition:  ' right ',  
         MOVEOPTIONS:&NBSP;FALSE,&NBSP
        labelsx:  ' to constituency ', 
        labeldx:  '  
   }; 
});  


MultiSelect provides settings for properties, Selectedposition: Select the location of the item, default to right, that is, the final get selected value is the list box on the right-hand side. Moveoptions: The move option, which can be used to sort the options, is of little practical use and is set to false. Labelsx and LABELDX are used to set the title of the left and right two list boxes, if you do not need to be set to empty. Maxselected: The maximum number of options, that is, only a few items can be selected

Full instance

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "keywords" content= "multiple-selection list box, jquery plugin"/>
<title> Operations multiple-selection list box </title>
<link rel= "stylesheet" type= text/css "href=". /css/main.css "/>
<link rel= "stylesheet" type= "Text/css" href= "Jquery.multiselect2side.css"/>
<style type= "Text/css" >
. demo{width:600px margin:40px Auto; color: #424242; background: #fff}
#sel {Overflow:hidden}
</style>
<script type= "Text/javascript" src= ". /js/jquery.js "></script>
<script type= "Text/javascript" src= "Jquery.multiselect2side.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#liOption"). Multiselect2side ({
Selectedposition: ' Right ',
Moveoptions:false,
LABELSX: ',
LABELDX: '
});
});
</script>

<body>


<div id= "Main" >
&LT;H2 class= "Top_title" ><a href= "http://www.helloweba.com/view-blog-58.html" >multiselect: Operating multiple-selection list box </a ><div class= "Demo" >
<form id= "Sel_form" action= "posts.php" method= "POST" >
<div id= "SEL" >
<select name= "lioption[]" id= ' lioption ' multiple= ' multiple ' size= ' 8 ' >
<option value= "PHP" >PHP</option>
<option value= "MYSQL" >MYSQL</option>
<option value= "asp.net" >ASP.NET</option>
<option value= "XHTML" >XHTML</option>
<option value= "CSS" >CSS</option>
<option value= "JQUERY" >JQUERY</option>
</select>
</div>
<input type= "Submit" class= "BTN" value= "submitted"/>
</form>
</div>

</div>

</body>


based on the above example, let's look at a php+mysql+jquery implementation query and list box selection operation

Xhtml

The code is as follows Copy Code

<form id= "Sel_form" action= "post.php" method= "POST" >
<p><input type= "text" name= "Keys" id= "Keys" value= "Enter your name or cell phone number" onclick= "this.value=" ""/>
<input type= "button" id= "SearchOption" value= "search/> <span" id= "Msg_ser" ></span></p>
<div id= "SEL" >
<select name= "lioption[]" id= ' lioption ' multiple= ' multiple ' size= ' 8 ' >
</select>
</div>
<input type= "Submit" value= "submitted"/>
</form>

Description, HTML content is a form that contains a query input box, and a list box, as well as related buttons.

MySQL data table structure

The code is as follows Copy Code

CREATE TABLE IF not EXISTS ' T_mult ' (
' id ' int (one) not NULL auto_increment,
' username ' varchar not NULL,
' Phone ' varchar not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;

Table T_mult is a contact sheet, including name and cell phone number fields.

Css

The code is as follows Copy Code

<link rel= "stylesheet" type= "Text/css" href= "Jquery.multiselect2side.css"/>

In this example, only the style files required for the MultiSelect plug-in are loaded, and other CSS can be designed by ourselves.

Javascript

First you need to reference the two JS files required by this example.

The code is as follows Copy Code

<script type= "Text/javascript" src= "Js/jquery.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.multiselect2side.js" ></script>
<link rel= "stylesheet" type= "Text/css" href= "Jquery.multiselect2side.css"/>

Then we'll call the MultiSelect plugin.

The code is as follows Copy Code

$ ("#liOption"). Multiselect2side ({
Selectedposition: ' Right ',
Moveoptions:false,
LABELSX: ' To be a constituency ',
LABELDX: ' Already elected '
});

About the application of MultiSelect, if unfamiliar to see before the article: MultiSelect: Action Multiple-selection list box

Then we write the search button to do the AJAX query data operation.

The code is as follows Copy Code

$ ("#searchOption"). Click (function () {
var keys=$ ("#keys"). Val ();
$.ajax ({
Type: "POST",
URL: "action.php",
Data: "Title=" +keys,
Success:function (msg) {
if (msg==1) {
$ ("#msg_ser"). Show (). HTML ("no record!") ");
}else{
$ ("#liOptionms2side__sx"). HTML (msg);
$ ("#msg_ser"). HTML ("");
}
}
});
$ ("#msg_ser"). Ajaxsend (function (event, request, settings) {
$ (this). html ("");
});
});

Description, when the Search button is clicked, the Ajax asynchronous operation, JavaScript will get the input value of the search box, Post to the background program action.php processing, processing, return different results to JavaScript, if the return 1,html page will prompt "No record", conversely, the result output to the left list box (to be selected): lioptionms2side__ Sx. Note that the key point is, why is the list box not XHTML lioption, and become lioptionms2side__sx? This to start from the MultiSelect plug-in, MultiSelect plug-in is actually a list box to replace the left and right two list box, has been for the relevant operations, by viewing its plug-in code is not difficult to find, the left of the list box named: LIOPTIONMS2SIDE__SX, The Right list box (the selection) is named: LIOPTIONMS2SIDE__DX, which is used later.

Php

First of all, look at the treatment of action.php.

The first step is to connect to the database.

The code is as follows Copy Code

$conn =mysql_connect ("localhost", "root", "");
mysql_select_db ("demo", $conn);
mysql_query ("SET names UTF8");

The second step reads the data and outputs it. By detecting the value from the search box, construct a different SQL statement and return the data to the output, the code is as follows:

The code is as follows Copy Code

$keys =trim ($_post[' title ']);  
$keys =mysql_real_escape_string ($keys, $conn);  
If!empty ($keys ) { 
         $sql = "select * from t_mult  where username like  '% $keys% '  or phone= ' $keys ';  
}else{ 
          $sql = "Select * from t_mult";  

$ Query=mysql_query ($sql);  
$count =mysql_num_rows ($query);  
if ($count >0) { 
       while ($row =mysql_fetch_array ($query)) { 
            $str. = "<option value= '". $row [' id ']. "' > ". $row [' username ']." -". $row [' phone ']." </option>  
    } 
    echo  $str; 
}else{ 
      echo  "1";  

Finally, there is a submit operation, and the background post.php the program to get the value of the final submitted item.

The code is as follows Copy Code

$selID =$_post[' Lioptionms2side__dx '];
if (!empty ($selID)) {
$str =implode (",", $selID);
Echo $str;
}else{
echo "did not select any items!" ";
}


Notice that we get the value of the LIOPTIONMS2SIDE__DX on the right list box, not the Lioption value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.