option to add operations (jquery implementation) between two select _jquery

Source: Internet
Author: User
Wrote a very simple jquery plugin, the function of which is called in the page can be implemented.
Plug-in source code (listtolist.js):
Copy Code code as follows:

/**
Fromid: ID of the source list.
Toid: ID of the destination list.
Moveorappend parameter ("Move" or "append"):
Move-the option selected in the source list is deleted. The option selected in the source list is moved to the destination list and is not added if the destination list already exists.
Append--the option selected in the source list is not deleted. The option selected in the source list is added after the destination list and is not added if the destination list already exists.

Isall parameter (TRUE or FALSE): whether to move or add all
*/
Jquery.listtolist = function (Fromid,toid,moveorappend,isall) {
if (moveorappend.tolowercase () = = "Move") {//Mobile
if (Isall = = true) {//All moves
$ ("#" +fromid+ "option"). each (function () {
Adds option from the source list to the destination list, and does nothing when the option is already in the destination list.
$ (this). Appendto ($ ("#" +toid+ ": Not (: Has (option[value=" +$ (This). Val () + "])));
});
$ ("#" +fromid). empty (); Empty source list
}
else if (Isall = False) {
$ ("#" +fromid+ "option:selected"). each (function () {
Adds option from the source list to the destination list, and does nothing when the option is already in the destination list.
$ (this). Appendto ($ ("#" +toid+ ": Not (: Has (option[value=" +$ (This). Val () + "])));
option that already exists in the target list is not moved and is still empty in the source list.
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 () {
$ ("<option></option>")
. Val ($ (this). Val ())
. Text ($ (this). Text ())
. Appendto ($ ("#" +toid+ ": Not (: Has (option[value=" +$ (This). Val () + "])));
});
}
else if (Isall = False) {
$ ("#" +fromid+ "option:selected"). each (function () {
$ ("<option></option>")
. Val ($ (this). Val ())
. Text ($ (this). Text ())
. Appendto ($ ("#" +toid+ ": Not (: Has (option[value=" +$ (This). Val () + "])));
});
}
}
};
/**
function is generally ditto ("move").
The difference is that option in the source list is not deleted when the Select option in the source list exists in the destination list.

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

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.