Adding options between two select statements (implemented by jquery) _ jquery

Source: Internet
Author: User
Select: add the selected select option to another select, or click Add all to add all options. I wrote a very simple jquery plug-in, which can be implemented by calling the functions on the page.
Plug-in source code (listtolist. js ):

The Code is as follows:


/**
Fromid: id of the source list.
Toid: id of the target list.
MoveOrAppend parameter ("move" or "append "):
Move -- the option selected in the source list will be deleted. The option selected in the source list will be moved to the target list. If the option already exists in the target list, this option will not be added.
Append -- the option selected in the source list is not deleted. The option selected in the source list is added to the end of the target list. If the option already exists in the target list, this option is not added.

IsAll parameter (true or false): whether to move or add all
*/
JQuery. listTolist = function (fromid, toid, moveOrAppend, isAll ){
If (moveOrAppend. toLowerCase () = "move") {// move
If (isAll = true) {// move all
$ ("#" + Fromid + "option"). each (function (){
// Add the option in the source list to the target list. If the option already exists in the target list, no operation is performed.
$ (This ). appendTo ($ ("#" + toid + ": not (: has (option [value =" + $ (this ). val () + "])");
});
$ ("#" + Fromid). empty (); // clear the source list
}
Else if (isAll = false ){
$ ("#" + Fromid + "option: selected"). each (function (){
// Add the option in the source list to the target list. If the option already exists in the target list, no operation is performed.
$ (This ). appendTo ($ ("#" + toid + ": not (: has (option [value =" + $ (this ). val () + "])");
// The option that already exists in the target list is not moved. It is still in the source list and cleared.
If ($ ("#" + fromid + "option [value =" + $ (this). val () + "]"). length> 0 ){
$ ("#" + Fromid). get (0)
. RemoveChild ($ ("#" + fromid + "option [value =" + $ (this). val () + "]"). get (0 ));
}
});
}
}
Else if (moveOrAppend. toLowerCase () = "append "){
If (isAll = true ){
$ ("#" + Fromid + "option"). each (function (){
$ ("")
. Val ($ (this). val ())
. Text ($ (this). text ())
. AppendTo ($ ("#" + toid + ": not (: has (option [value =" + $ (this ). val () + "])");
});
}
Else if (isAll = false ){
$ ("#" + Fromid + "option: selected"). each (function (){
$ ("")
. Val ($ (this). val ())
. Text ($ (this). text ())
. AppendTo ($ ("#" + toid + ": not (: has (option [value =" + $ (this ). val () + "])");
});
}
}
};
/**
The function is similar to the above ("move ").
The difference is that when the selected option in the source list exists in the target list, the option in the source list will not be deleted.

IsAll parameter (true or false): whether to move or add all
*/
JQuery. list2list = function (fromid, toid, isAll ){
If (isAll = true ){
$ ("#" + Fromid + "option"). each (function (){
$ (This ). appendTo ($ ("#" + toid + ": not (: has (option [value =" + $ (this ). val () + "])");
});
}
Else if (isAll = false ){
$ ("#" + Fromid + "option: selected"). each (function (){
$ (This ). appendTo ($ ("#" + toid + ": not (: has (option [value =" + $ (this ). val () + "])");
});
}
};


Package download
Related Article

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.