转自:http://blog.csdn.net/kuangmiao1120/archive/2009/11/10/4793887.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="UTF-8" />
<meta http-equiv="expires" content="0">
<meta name="author" content="Jaffe(onlea.com)" />
<meta http-equiv="keywords" content="JS,Option,增加,删除,上下移动,onlea,绿野飞扬">
<meta http-equiv="description" content="JS控制Option实例(增加、删除,上下移动">
<title>JS控制Option实例(增加、删除,上下移动) - 绿野飞扬</title>
<style type=text/css>
<!--
body,div,td,input,legend {
font-size:12px;
color:#47391f;
font-family:Tahoma,Arial,sans-serif,宋体
}
fieldset,form{
margin:0;
}
a{
color:#66aa66;
text-decoration:none;
}
a:hover{
color:#99aa66
}
#container{
text-align:center;
}
#divBody,#divFooter{
text-align:center;
width:400px;
margin:10px auto;
}
#divFooter{
color:#999;
}
-->
</style>
</head>
<body>
<div id="container">
<div id="divBody">
<form action="">
<fieldset>
<legend>JS控制Option实例(增加、删除,上下移动)</legend>
<div style="margin:15px auto;">
<table align="center">
<tr>
<td>
<div><b>Item Properties</b></div>
<select size="15" id="selectleft" style="width:150px">
<option value="1">Properties 1</option>
<option value="2">Properties 2</option>
<option value="3">Properties 3</option>
<option value="4">Properties 4</option>
<option value="5">Properties 5</option>
<option value="6">Properties 6</option>
<option value="7">Properties 7</option>
<option value="8">Properties 8</option>
</select>
</td>
<td valign="middle" align="center">
<input value=" 增加 " type="button" id="addto" /><br />
<input value=" 删除 " type="button" id="moveback" /><br /><br />
<input value=" 顶部 " type="button" id="moveTop" /><br />
<input value=" 上移 " type="button" id="moveUp"><br />
<input value=" 下移 " type="button" id="moveDown"><br />
<input value=" 底部 " type="button" id="moveBottom" /><br />
</td>
<td>
<div><b>Properties You Chosen</b></div>
<select size="15" id="selectright" style="width:150px">
</select>
</td>
</tr>
</table>
</div>
</fieldset>
</form>
</div>
<script type="text/javascript">
<!--
var selectLeft = document.getElementById("selectleft");
var selectRight = document.getElementById("selectright");
var addTo = document.getElementById("addto");
var moveBack = document.getElementById("moveback");
var moveTop = document.getElementById("moveTop");
var moveUp = document.getElementById("moveUp");
var moveDown = document.getElementById("moveDown");
var moveBottom = document.getElementById("moveBottom");
addTo.onclick = addOption;
moveBack.onclick = delOption;
moveTop.onclick = mTop;
moveUp.onclick = mUp;
moveDown.onclick = mDown;
moveBottom.onclick = mBottom;
//这个函数检验传入的值是否在有边出现过!
function hasOption(str){
for(var i=0;i<selectRight.options.length;i++){
if(selectRight.options[i].value==str){
return false;
}
}
return true;
}
function addOption(){
var nowIndex = selectRight.options.length; //右边的下一个索引
var selectIndex = selectLeft.options.selectedIndex; //左边被选种项索引
if(selectIndex != -1){//如果选了一项,执行
var selectText = selectLeft.options[selectIndex].text; //被选种项文本
var selectValue = selectLeft.options[selectIndex].value; //被选种项值
if(hasOption(selectValue)){//如果选中的项目右边没有,执行
var newoption = new Option(selectText,selectValue,false,false);
selectRight.options[nowIndex] = newoption;
}else{
alert("该选项已经存在于右边列表中");
}
}else{
alert("请从左边列表中选择一个选项");
}
}
function delOption(){
var selectIndex = selectRight.options.selectedIndex;
if(selectIndex!=-1){
selectRight.options[selectIndex] = null; //清空选种项
}
}
function mTop(){
var i = selectRight.options.selectedIndex;
if(i > 0){
Temp_Text=selectRight.options(i).text;
Temp_ID=selectRight.options(i).value;
for(j=i;j>0;j--){
selectRight.options(j).text=selectRight.options(j-1).text;
selectRight.options(j).value=selectRight.options(j-1).value;
}
selectRight.options(0).value=Temp_ID;
selectRight.options(0).text=Temp_Text;
selectRight.selectedIndex=0;
}
}
function mUp(){
var i = selectRight.options.selectedIndex;
var j = i-1
if(i>0){
Temp_Text = selectRight.options(j).text;
Temp_ID = selectRight.options(j).value;
selectRight.options(j).text = selectRight.options(i).text;
selectRight.options(j).value = selectRight.options(i).value;
selectRight.options(i).text = Temp_Text;
selectRight.options(i).value = Temp_ID;
selectRight.selectedIndex=j;
}
}
function mDown(){
var i = selectRight.options.selectedIndex;
if (i != selectRight.length-1){
var j = i+1;
if(i < selectRight.length){
Temp_Text = selectRight.options(j).text;
Temp_ID = selectRight.options(j).value;
selectRight.options(j).text = selectRight.options(i).text;
selectRight.options(j).value = selectRight.options(i).value;
selectRight.options(i).text = Temp_Text;
selectRight.options(i).value = Temp_ID;
selectRight.selectedIndex=j;
}
}
}
function mBottom(){
var i = selectRight.selectedIndex;
var j = selectRight.length-1
if(i < j){
Temp_Text = selectRight.options(i).text;
Temp_ID = selectRight.options(i).value;
for(var k=i+1;k<=j;k++){
selectRight.options(k-1).text=selectRight.options(k).text;
selectRight.options(k-1).value=selectRight.options(k).value;
}
selectRight.options(j).text=Temp_Text;
selectRight.options(j).value=Temp_ID;
selectRight.selectedIndex=j;
}
}
//-->
</script>
</div>
</body>
</html>
js动态添加、删除select的option
<select id="ddlResourceType" onchange="getvalue(this)">
</select>
动态删除select中的所有options:
document.getElementById("ddlResourceType").options.length=0;
动态删除select中的某一项option:
document.getElementById("ddlResourceType").options.remove(indx);
动态添加select中的项option:
document.getElementById("ddlResourceType").options.add(new Option(text,value));
分享到:
相关推荐
本文实例讲述了JS实现Select的option上下移动的方法。分享给大家供大家参考,具体如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> [removed] function UpOr...
实例038 动态输出JavaScript代码 69 实例039 当数字遇到了字符串 70 实例040 PHP程序员的基础——变量的应用 72 实例041 打印系统环境变量信息print_r($_ENV) 73 实例042 使用可变变量输出“I Like PHP!” 73 实例...
本文实例讲述了JavaScript实现下拉列表框数据增加、删除、上下排序的方法。分享给大家供大家参考。具体如下: 这里实现在一个支持多选的下拉列表框内进行数据项的添加、删除、向上、向下移动操作,我们在一些人才...
本文实例讲述了基于javascript实现单选及多选的向右和向左移动实例。分享给大家供大家参考。具体实现方法如下: 方法 一: <body> 实现单选及多选的向右和向左移动 <select id=lselect size=10 ...
实例038 动态输出JavaScript代码 69 实例039 当数字遇到了字符串 70 实例040 PHP程序员的基础——变量的应用 72 实例041 打印系统环境变量信息print_r($_ENV) 73 实例042 使用可变变量输出“I Like PHP!” 73 实例...
select选择框内容左右移动添加删除代码基于jquery-1.8.3.min.js实现,简单实用,选中选项内容,点击移动按钮可进行内容左右移动,双击option内容也可左右移动,支持单选移动、多选移动和一键全部移动! 运行效果图:...
本文实例讲解了javascript实现listbox左右移动的详细代码,分享给大家供大家参考,具体内容如下 效果图: 具体代码: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=gb...
本案例是参考cropper站点实例,进行修改简化。 option相关参数说明: viewMode 显示模式 Type: Number Default: 0 Options: 0: the crop box is just within the container 裁剪框只能在 1内移动 1: the crop ...
项目要实现这样的一个功能(如下图所示):选择左边下拉列表框中的选项,点击添加按钮,把选择的选项移动到右边的下拉列表框中,同样的选择右边的选项,点击删除按钮,即把选择的选项移动到左边的下拉列表框中.相信用js很多...
uniapp微信小程序使用Echarts组件,使用实例 <echarts class="uni-ec-canvas" ref="echarts2" :option="option" canvasId="echarts1" id='echarts2'></echarts>
本文实例讲述了jquery调取json数据实现省市级联的方法。分享给大家供大家参考。具体如下: 使用jQuery mobile作为创建移动web的框架,需要实现省市级联的功能,具体代码如下(还需要优化的地方): Html代码: ...
本文实例讲述了微信小程序实现动态设置placeholder提示文字及按钮选中取消状态的方法。分享给大家供大家参考,具体如下: 效果图展示 通过绑定点击事件placeholder方法,获取data-num的值,通过js判断num等于几,...
JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. 在JavaScript中,正则表达式是由一个RegExp对象表示的.当然,可以使用一个RegExp()构造函数来创建RegExp...
{12.5}操作符与实例}{154}{section.12.5} {12.5.1}where}{154}{subsection.12.5.1} {12.6}函数}{156}{section.12.6} {12.7}组函数}{158}{section.12.7} {12.7.1}group by}{159}{subsection.12.7.1} {12.7.2}...
网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R,修复! Windows XP(包括 ...JS:javascript源文件 JSP:HTML网页,其中包含有对一个Java servlet...