Java代ç
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>grid</title>
</head>
<body>
<input type="button" value="getSelect" onclick = "getSelect()"/>
<input type="button" value="selectbtn" onclick = "getSelAge()"/>
<div>
<select name="selectAge" id="selectAge">
<option value="1">18-21</option>
<option value="2">22-25</option>
<option value="3">26-29</option>
<option value="4">30-35</option>
<option value="5">Over35</option>
</select>
</div>
<p>
<input type="button" value="moreSelect" onclick = "moreSelect()"/>
<div>
<div>å¤é éè¦å¢å multipleå±æ§<br>
å¨å¤éä¸sizeå±æ§ å¯ä»¥åå§åä¸ææ¡é»è®¤æ¾ç¤ºå 个é项
</div>
<div>
<select name="moreselAge" id="moreselAge" multiple="multiple">
<option value="1">18-21</option>
<option value="2">22-25</option>
<option value="3">26-29</option>
<option value="4">30-35</option>
<option value="5">Over35</option>
<option value="6">Over40</option>
<option value="7">Over50</option>
</select>
</div>
</div>
<p></p>
<input type="button" value="addNewbtn" onclick = "addNewSelections()"/>
<input type="button" value="deletebtn" onclick = "deleteselections()"/>
<input type="button" value="deleAllbtn" onclick = "deleteAllSelections()"/>
<div>selectName :<input type="text" id="txtName"/></div>
<div>selectValue:<input type="text" id="txtValue"/></div>
<div>
<select name="moreselAge" id="addNew">
<option value="1" selected>18-21</option>
<option value="2">22-25</option>
<option value="3">26-29</option>
<option value="4">30-35</option>
<option value="5">Over35</option>
<option value="6">Over40</option>
<option value="7">Over50</option>
</select>
</div>
<p>移å¨é项</p>
<p>
<table>
<tr collspan="2">
<td>
<div>
<select name="moreselAge" id="move1" multiple="multiple" size="7">
<option value="1">18-21sfiods</option>
<option value="2">22-25sjdfd</option>
<option value="3">26-29xxs</option>
<option value="4">30-35vs</option>
<option value="5">Over35dcff</option>
<option value="6">Over40shhfsd</option>
<option value="7">Over50sdefs</option>
<option value="8">Over88www</option>
</select>
</div>
</td>
<td width="100" align="center">
<input type="button" value=">" onclick = "rightSingle()" /><br>
<input type="button" value=">>" onclick = "rightAll()"/><br>
<input type="button" value="<" onclick = "leftSingle()"/><br>
<input type="button" value="<<" onclick = "leftAll()"/>
</td>
<td>
<div>
<select name="moreselAge" id="move2" multiple="multiple" size="7">
<option value="1">18-21</option>
<option value="2">22-25</option>
<option value="3">26-29</option>
<option value="4">30-35</option>
<option value="5">Over35</option>
<option value="6">Over40</option>
<option value="7">Over50</option>
<option value="8">Over88</option>
</select>
</div>
</td>
<tr>
</table>
</body>
<script type="text/javascript">
//è·å¾ä¸æå表对象
oListbox = document.getElementById("selectAge");
var ListUtil = new Object();
var selectbtn = document.getElementById("selectbtn");
function getSelAge (){
//访é®é项
alert(oListbox.options[1].firstChild.nodeValue); //æ¾ç¤ºçå
容
alert(oListbox.options[1].getAttribute("value"));//对åºçvalue
alert("è·å¾å®å¨éåä¸çä½ç½®== " + oListbox.options[2].index); //è·å¾å®å¨éåä¸çä½ç½®
alert("è·å¾éåçå
ç´ ä¸ªæ°é¿åº¦== " + oListbox.options.length); //è·å¾éåçå
ç´ ä¸ªæ°é¿åº¦
}
/*************************************************************************************************/
//è·å¾éä¸é项
function getSelect(){
var indx = oListbox.selectedIndex;
alert("è·å¾éä¸çé项çç´¢å¼ "+ indx );
}
//å¤éä¸ææ¡
var moreselAgeList = document.getElementById("moreselAge");
/*******************************************************************/
//å
¥å ä¸ææ¡å¯¹è±¡
ListUtil.getSelectIndexes = function (oListbox){
var arrIndexes = new Array();
for(var i=0 ; i<oListbox.options.length;i++){
//å¦æ该项被éä¸åæ该项对åºçç´¢å¼æ·»å å°æ°ç»ä¸
if(oListbox.options[i].selected){
arrIndexes.push(i);
}
}
return arrIndexes; //è¿åéä¸çé项索å¼
}
/***************************************************************/
// å¤é
function moreSelect(){
var arrIndexes = ListUtil.getSelectIndexes(moreselAgeList);
alert("éä¸çæ°ç»length = "+ arrIndexes.length + " éä¸çé项索å¼ä¸º ï¼"+ arrIndexes);
}
/************************æ·»å æ°é项***************************************************************/
//
var addNewLisbox = document.getElementById("addNew"); //è·å¾ä¸ææ¡å¯¹è±¡
var otxtName = document.getElementById("txtName"); //name ææ¬æ¡
var otxtValue = document.getElementById("txtValue"); //value ææ¬æ¡
//æ·»å æ¹æ³
ListUtil.addOptions = function(oListbox,sName,sValue){
var arryV = new Array();
//æ è®°è¾å
¥çå¼æ¯å¦å¯ä»¥æ·»å
var isAdd = false;
//å¤ææ¯å¦æéå¤çå¼
for(var i =0 ;i<oListbox.options.length;i++){
var sv = oListbox.options[i].getAttribute("value");
if(sv == sValue){
alert("ä¸è½æ·»å éå¤çvalue");
return ;
}else{
isAdd = true;
}
}
if(isAdd || oListbox.options.length == 0){
//ä¸é¢ä½¿ç¨domæ¹æ³å建èç¹
var oOption = document.createElement("option");// å建optionå
ç´
oOption.appendChild(document.createTextNode(sName));
//å 为é项çå¼ä¸æ¯å¿
é¡»çï¼æ以å¦æä¼ å
¥äºå¼ åæ·»å è¿æ¥
if(arguments.length == 3){
oOption.setAttribute("value",sValue);
}
oListbox.appendChild(oOption); //æé项添å è¿å表æ¡
alert("æ·»å æå!!");
} // end if(isAdd)
}
//æ·»å æé®çç¹å»äºä»¶æ¹æ³
function addNewSelections(){
var txtname = otxtName.value;
var txtvalue = otxtValue.value;
if(txtname != "" && txtvalue != ""){
ListUtil.addOptions(addNewLisbox,txtname,txtvalue);//æ·»å æ°é¡¹
otxtName.value = "";
otxtValue.value = "";
}else{
alert("请è¾å
¥è¦æ·»å çå¼åname");
return;
}
}
/*******************å é¤éä¸é项****************************************************************/
//ä¼ å
¥ä¸ææ¡å¯¹è±¡å(ç´¢å¼)
ListUtil.deleteOptons = function(oListbox){
var selIndex = oListbox.selectedIndex;
if(oListbox.options.length == 0){
alert("å表ä¸æ å
ç´ å¯å é¤");
return ;
}
oListbox.remove(selIndex); //å é¤éä¸çé项
}
//å é¤æé®ç¹å»äºä»¶
function deleteselections(){
ListUtil.deleteOptons(addNewLisbox);
}
/**********å é¤ææ***********************************************************************/
ListUtil.deletsAllOptions = function(oListbox){
if(oListbox.options.length != 0){
for(var i= oListbox.options.length-1;i>=0;i--){ //åçå é¤æ¯å 为
oListbox.remove(i);
}
}else{
alert("该å表为空!");
}
}
function deleteAllSelections(){
ListUtil.deletsAllOptions(addNewLisbox);
}
/*******移å¨é项***************************************************************************************/
//è·å¾ä¸ææ¡
var move1Listbox = document.getElementById("move1"); //左边ä¸ææ¡
var move2Listbox = document.getElementById("move2"); //å³è¾¹ä¸ææ¡
//移å¨ä¸ä¸ªæå¤ä¸ªéä¸çé项
ListUtil.move = function(oListboxFrom ,oListboxTo){
//var idx1 = oListboxFrom.selectedIndex;
var arrIndexes = ListUtil.getSelectIndexes(oListboxFrom);
var oOption ;
if(arrIndexes.length == 0 ){
alert("请éæ©è³å°ä¸ä¸ªé项!");
return ;
}else{
for(var i=oListboxFrom.options.length-1;i>=0;i--){
oOption = oListboxFrom.options[i];
if(oOption.selected && oOption != null ){
oListboxTo.appendChild(oOption);
}
}
}
}
//åå³ç§» ä¸ä¸ªå
ç´
function rightSingle(){
ListUtil.move(move1Listbox,move2Listbox);
};
//å左移 ä¸ä¸ªå
ç´
function leftSingle(){
ListUtil.move(move2Listbox,move1Listbox);
}
ListUtil.moveAll = function(oListboxFrom,oListboxTo){
for(var i=oListboxFrom.options.length-1;i>=0;i--){
oOption = oListboxFrom.options[i];
//alert(oOption);
oListboxTo.appendChild(oOption);
}
}
//åå³ç§»ææé项
function rightAll(){
ListUtil.moveAll(move1Listbox,move2Listbox);
}
//å左移ææé项
function leftAll(){
ListUtil.moveAll(move2Listbox,move1Listbox);
}
</script>
</html>
温馨提示:答案为网友推荐,仅供参考