悟心

          成功不是將來才有的,而是從決定去做的那一刻起,持續累積而成。 上人生的旅途罷。前途很遠,也很暗。然而不要怕。不怕的人的面前才有路。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            93 隨筆 :: 1 文章 :: 103 評論 :: 0 Trackbacks
          <2016年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(2)

          隨筆分類(91)

          隨筆檔案(92)

          文章檔案(1)

          新聞檔案(5)

          相冊

          1

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          獲取顯示的漢字

          document.getElementById("bigclass").options[window.document.getElementById("bigclass").selectedIndex].text

          獲取數據庫中的id

          window.document.getElementById("bigclass").value

          獲取select組分配的索引id

          window.document.getElementById("bigclass").selectedIndex


          例子:

          <select name="bigclass" id="bigclass" onChange="javascript:updatePage2();">
          <option value="" selected="selected">ajax實驗</option>
          <option value="4">我適宜市哈</option>
          </select>


          使用
          document.getElementById("bigclass").options[window.document.getElementById("bigclass").selectedIndex].text
          的結果是:我適宜市哈

          使用
          window.document.getElementById("bigclass").value
          的結果是:4

          使用
          window.document.getElementById("bigclass").selectedIndex
          的結果是:1

          一、新增一個option
              var sel=document.getElementById("select的id");
              var op=document.createElement("option");
               op.value=值;
               op.text=顯示文本;
               sel.add(op);

          二、刪除一個option
              var sel=document.getElementById("typelist");
          if(sel.selectedIndex==-1)
             alert("請選中要刪除的項!");
          for(var i=0;i<sel.options.length;i++){
          if(sel.options[i].selected){
          sel.options.remove(i);
          break;
          }
          }

          三、清空select的所有option
             var citySel=document.getElementById("select的id");
             citySel.options.length=0;
            

          四、獲得選中項的值
              var citySel=document.getElementById("select的id");
              var selectedValue=citySel.value;


          五、獲得當前選中項的索引
              var selectedIndex=document.all.objSelect.selectedIndex;

          六、設置select的當前選中項
              方法1(單個select): document.getElementById("products_type_id").selectedIndex=1;
              方法2(級聯select如省市級聯):
               var province_sel=document.getElementById("province");//獲得省select
          var city_sel=document.getElementById("city");//獲得市select
          for(var i=0;i<province_sel.options.length;i++){
          if(province_sel.options[i].value=="從數據庫獲取的省的值"){
              province_sel.options[i].selected=true;
          break;
          }
          }
          initCity("從數據庫獲取的省的值");//初始化市select
          for(var i=0;i<city_sel.options.length;i++){
          if(city_sel.options[i].value=="${city}"){
              city_sel.options[i].selected=true;
          break;
          }
          }

          七、創建select動態設置選中項
             var sel=document.getElementById("other_state");
               var sel_val=document.getElementById("other_media_id").innerHTML;
              
          for(var obj in data){
          var id=data[obj]["other_media_id"];  
          var name=data[obj]["other_media_name"];
          var op=document.createElement("option");
          op.setAttribute("value",id);
          op.appendChild(document.createTextNode(name));     
                     if(id==sel_val){
          op.setAttribute("selected","true");
             }
                      sel.appendChild(op);
          }

          1、向Select里添加Option
          function fnAddItem(text,value)
                  {
                      var selTarget = document.getElementById("selID");

                      selTarget.Add(new Option("text","value"));
                  }


          2、刪除Select里的Option
          function fnRemoveItem()
                  {
                      var selTarget = document.getElementById("selID");

                      if(selTarget.selectedIndex > -1)
                      {//說明選中
                          for(var i=0;i<selTarget.options.length;i++)
                          {
                              if(selTarget.options[i].selected)
                              {
                                  selTarget.remove(i);
                                 
                                  i = i - 1;//注意這一行
                              }
                          }
                      }
                  }
          3、移動Select里的Option到另一個Select中
                  function fnMove(fromSelectID,toSelectID)
                  {
                      var from = document.getElementById(fromSelectID);
                      var to = document.getElementById(toSelectID);
                     
                      for(var i=0;i<from.options.length;i++)
                      {
                          if(from.options[i].selected)
                          {
                              to.appendChild(from.options[i]);
                              i = i - 1;
                          }
                      }
                  }
              if 里的代碼也可用下面幾句代碼代替


          var op = from.options[i];
          to.options.add(new Option(op.text, op.value));
          from.remove(i);
          4、Select里Option的上下移動
                  function fnUp()
                  {  
                      var sel = document.getElementById("selID");
                      for(var i=1; i < sel.length; i++)
                      {//最上面的一個不需要移動,所以直接從i=1開始
                          if(sel.options[i].selected)
                          {
                              if(!sel.options.item(i-1).selected)
                              {//上面的一項沒選中,上下交換
                                    var selText = sel.options[i].text;
                                    var selValue = sel.options[i].value;
                                   
                                    sel.options[i].text = sel.options[i-1].text;
                                    sel.options[i].value = sel.options[i-1].value;
                                    sel.options[i].selected = false;
                                   
                                    sel.options[i-1].text = selText;
                                    sel.options[i-1].value = selValue;
                                    sel.options[i-1].selected=true;
                              }
                          }
                      }
                  }
          在進行上下兩項互換時,也可以使用以下代碼,但是效率很低,因為每一次的Dom操作都將導致整個頁面的重新布局,所以不如直接修改元素的屬性值。

                                  var oOption = sel.options[i]
                                  var oPrevOption = sel.options[i-1]
                                  sel.insertBefore(oOption,oPrevOption);
          向下移動同理

          function fnDown()
                  {
                      var sel = fnGetTarget("selLeftOrRight");
                      for(var i=sel.length -2; i >= 0; i--)
                      {//向下移動,最后一個不需要處理,所以直接從倒數第二個開始
                          if(sel.options.item(i).selected)
                          {
                              if(!sel.options.item(i+1).selected)
                              {//下面的Option沒選中,上下互換
                                    var selText = sel.options.item(i).text;
                                    var selValue = sel.options.item(i).value;
                                   
                                    sel.options.item(i).text = sel.options.item(i+1).text;
                                    sel.options.item(i).value = sel.options.item(i+1).value;
                                    sel.options.item(i).selected = false;
                                   
                                    sel.options.item(i+1).text = selText;
                                    sel.options.item(i+1).value = selValue;
                                    sel.options.item(i+1).selected=true;
                              }
                          }
                      }
                  }
          5、Select里Option的排序
          這里借助Array對象的sort方法進行操作,sort方法接受一個function參數,可以在這個function里定義排序時使用的算法邏輯。
          array.sort([compareFunction]) 里compareFunction接受兩個參數(p1,p2),sort操作進行時,array對象會每次傳兩個值進去,進行比較;compareFunciton必須返回一個整數值:當返回值>0時,p1會排在p2后面;返回值<0時,p1會排在p2前面;返回值=0時,不進行操作。
          例如:

          function fnCompare(a,b)
                  {
                      if (a < b)
                          return -1;
                      if (a > b)
                          return 1;
                      return 0;
                  }
          var arr = new Array();
          //add some value into arr
          arr.sort(fnCompare);
          //這里sort的操作結果就是arr里的項按由小到大的升序排序
          //如果把fnCompare里改為
          //if (a < b)
          // return 1;
          //if (a > b)
          // return -1;
          //return 0;
          //則sort的結果是降序排列

          好,下面就是對Select里Option的排序

          //因為排序可以按Option的Value排序,也可以按Text排序,這里只演示按Value排序
          function sortItem()
          {
              var sel = document.getElementById("selID");
              var selLength = sel.options.length;
              var arr = new Array();
              var arrLength;

              //將所有Option放入array
              for(var i=0;i<selLength;i++)
              {
                  arr[i] = sel.options[i];
              }
              arrLength = arr.length;

              arr.sort(fnSortByValue);//排序
              //先將原先的Option刪除
              while(selLength--)
              {
                  sel.options[selLength] = null;
              }
              //將經過排序的Option放回Select中
              for(i=0;i<arrLength;i++)
              {
                  sel.add(new Option(arr[i].text,arr[i].value));
              }
          }
          function fnSortByValue(a,b)
          {
              var aComp = a.value.toString();
              var bComp = b.value.toString();

              if (aComp < bComp)
                  return -1;
              if (aComp > bComp)
                  return 1;
              return 0;
          }
          排序時還可以有更多選項,比如將value值看做Integer或是String進行排序,得到的結果是不一樣的。篇幅限制,不在多做介紹。
          我將這些所有的操作都寫在了一個文件里,運行的效果如圖(點擊看大圖)


          有興趣的朋友可以下載來看看,里面還設計div+css排版等。

          posted on 2009-11-12 23:24 艾波 閱讀(18984) 評論(1)  編輯  收藏 所屬分類: Javascript

          評論

          # re: javascript獲取select的值全解 2016-05-09 00:54 企鵝
          公式  回復  更多評論
            

          主站蜘蛛池模板: 托克逊县| 邓州市| 铜鼓县| 潜江市| 龙口市| 连江县| 北辰区| 临沂市| 荔波县| 广丰县| 普兰县| 博白县| 麻阳| 禹城市| 章丘市| 广汉市| 新和县| 达拉特旗| 新疆| 晋城| 南靖县| 枣强县| 襄垣县| 景东| 安多县| 乐亭县| 阿克苏市| 察哈| 攀枝花市| 大新县| 兴化市| 克山县| 治多县| 安徽省| 襄汾县| 青神县| 太仆寺旗| 鄂州市| 大竹县| 通辽市| 通榆县|