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