尽管使用EXT开发了一段时间,可是自己认为我对javascript还是不是非常熟,所以边看书边做小样例
给自己以后用到的时候查看下,都是非常主要的东西,对刚開始学习的人可能有点帮助
以下是代码
- <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"));
-
- 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");
- var otxtValue = document.getElementById("txtValue");
-
-
-
- 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){
-
-
- var oOption = document.createElement("option");
- oOption.appendChild(document.createTextNode(sName));
-
-
- if(arguments.length == 3){
- oOption.setAttribute("value",sValue);
- }
- oListbox.appendChild(oOption);
- alert("加入成功!!");
-
- }
-
-
- }
-
-
- 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 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];
-
- oListboxTo.appendChild(oOption);
- }
- }
-
-
- function rightAll(){
-
- ListUtil.moveAll(move1Listbox,move2Listbox);
- }
-
-
- function leftAll(){
- ListUtil.moveAll(move2Listbox,move1Listbox);
- }
-
-
-
- </script>
- </html>