js select多选列表传值代码
程序员文章站
2023-12-12 19:38:46
js select
/*移除左边选中的列表项到右边*/
function fmoveselectedoptionslefttoright(oleft,or...
[ctrl+a 全选 注:如需引入外部js需刷新才能执行]
js核心代码
复制代码 代码如下:
/*移除左边选中的列表项到右边*/
function fmoveselectedoptionslefttoright(oleft,oright)
{
if(!(oleft&&oright))
{
return;
}
if(!hasoptions(oleft))
{
return;
}
if(oleft.selectedindex==-1)
{
oleft.selectedindex=0;
}
for(var i=0;i<oleft.options.length;i++)
{
if(oleft.options[i].selected)
{
var ooption = document.createelement("option");
ooption.setattribute("text",oleft.options[i].text);
ooption.setattribute("value",oleft.options[i].value);
oright.add(ooption);
}
}
clearselectedoptions(oleft);
}
/*移除左边的所有列表项到右边*/
function fmovealloptionslefttoright(oleft,oright)
{
if(!(oleft&&oright))
{
return;
}
if(!hasoptions(oleft))
{
return;
}
for(var i=0;i<oleft.options.length;i++)
{
var ooption = document.createelement("option");
ooption.setattribute("text",oleft.options[i].text);
ooption.setattribute("value",oleft.options[i].value);
oright.add(ooption);
}
clearalloptions(oleft);
}
/*移除右边选中的列表项到左边*/
function fmoveselectedoptionsrighttoleft(oleft,oright)
{
if(!(oleft&&oright))
{
return;
}
if(!hasoptions(oright))
{
return;
}
if(oright.selectedindex==-1)
{
oright.selectedindex=0;
}
for(var i=0;i<oright.options.length;i++)
{
if(oright.options[i].selected)
{
var ooption = document.createelement("option");
ooption.setattribute("text",oright.options[i].text);
ooption.setattribute("value",oright.options[i].value);
oleft.add(ooption);
}
}
clearselectedoptions(oright);
}
/*移除右边的所有列表项到左边*/
function fmovealloptionsrighttoleft(oleft,oright)
{
if(!(oleft&&oright))
{
return;
}
if(!hasoptions(oright))
{
return;
}
for(var i=0;i<oright.options.length;i++)
{
var ooption = document.createelement("option");
ooption.setattribute("text",oright.options[i].text);
ooption.setattribute("value",oright.options[i].value);
oleft.add(ooption);
}
clearalloptions(oright);
}
/*清空select所有options*/
function clearalloptions(oselect)
{
if(oselect)
{
var ops=oselect.options;
while(ops.length>0)
{
oselect.remove(ops.length-1);
}
}
}
/*清空select所有选中的options*/
function clearselectedoptions(oselect)
{
if(oselect)
{
for(var i=0;i<oselect.options.length;i++)
{
if(oselect.options[i].selected)
{
oselect.remove(i--);
}
}
}
}
/*判断select是否有options*/
function hasoptions(oselect)
{
if(oselect)
{
return oselect.options.length>0;
}
return false;
}