阿里妈妈,帮你实现网络赚钱梦,流量变成现金!
收藏几个JS代码,Listbox(列表框)的全选,添加删除,上移下移的JS函数1.这个是全选的函数
复制内容到剪贴板
程序代码
程序代码 function listbox_selectall(listID, isSelect) {
var listbox = document.getElementById(listID);
for(var count=0; count < listbox.options.length; count++) {
listbox.options[count].selected = isSelect;
}
}
var listbox = document.getElementById(listID);
for(var count=0; count < listbox.options.length; count++) {
listbox.options[count].selected = isSelect;
}
}
使用方法:
listbox_selectall('list', true); //全选
listbox_selectall('list', false); //取消全选
2.上移下移的函数
复制内容到剪贴板
程序代码
程序代码function listbox_move(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
使用方法,只需要传递Listbox的ID和移动的方向(up或down):
listbox_move('list', 'up'); //将所选项上移
listbox_move('ist', 'down'); //将所选项下移
3.将Listbox选项移到另一个Listbox中
复制内容到剪贴板
程序代码
程序代码function listbox_moveacross(sourceID, destID) {
var src = document.getElementById(sourceID);
var dest = document.getElementById(destID);
for(var count=0; count < src.options.length; count++) {
if(src.options[count].selected == true) {
var option = src.options[count];
var newOption = document.createElement("option");
newOption.value = option.value;
newOption.text = option.text;
newOption.selected = true;
try {
dest.add(newOption, null); //标准
src.remove(count, null);
}catch(error) {
dest.add(newOption); // 针对IE的情况
src.remove(count);
}
count--;
}
}
}
var src = document.getElementById(sourceID);
var dest = document.getElementById(destID);
for(var count=0; count < src.options.length; count++) {
if(src.options[count].selected == true) {
var option = src.options[count];
var newOption = document.createElement("option");
newOption.value = option.value;
newOption.text = option.text;
newOption.selected = true;
try {
dest.add(newOption, null); //标准
src.remove(count, null);
}catch(error) {
dest.add(newOption); // 针对IE的情况
src.remove(count);
}
count--;
}
}
}
使用方法,传递两个参数,源Listbox的ID和目的Listbox的ID:
listbox_moveacross('srclist', 'destlist');
发表评论
上一篇:
下一篇: 
文章来自:
文章标签:
网摘收录: