悟心

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            93 隨筆 :: 1 文章 :: 103 評論 :: 0 Trackbacks

          獲取顯示的漢字

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

          獲取數(shù)據(jù)庫中的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實(shí)驗(yàn)</option>
          <option value="4">我適宜市哈</option>
          </select>


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

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

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

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

          二、刪除一個(gè)option
              var sel=document.getElementById("typelist");
          if(sel.selectedIndex==-1)
             alert("請選中要?jiǎng)h除的項(xiàng)!");
          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;
            

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


          五、獲得當(dāng)前選中項(xiàng)的索引
              var selectedIndex=document.all.objSelect.selectedIndex;

          六、設(shè)置select的當(dāng)前選中項(xiàng)
              方法1(單個(gè)select): document.getElementById("products_type_id").selectedIndex=1;
              方法2(級聯(lián)select如省市級聯(lián)):
               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=="從數(shù)據(jù)庫獲取的省的值"){
              province_sel.options[i].selected=true;
          break;
          }
          }
          initCity("從數(shù)據(jù)庫獲取的省的值");//初始化市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;
          }
          }

          七、創(chuàng)建select動(dòng)態(tài)設(shè)置選中項(xiàng)
             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、移動(dòng)Select里的Option到另一個(gè)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的上下移動(dòng)
                  function fnUp()
                  {  
                      var sel = document.getElementById("selID");
                      for(var i=1; i < sel.length; i++)
                      {//最上面的一個(gè)不需要移動(dòng),所以直接從i=1開始
                          if(sel.options[i].selected)
                          {
                              if(!sel.options.item(i-1).selected)
                              {//上面的一項(xiàng)沒選中,上下交換
                                    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;
                              }
                          }
                      }
                  }
          在進(jìn)行上下兩項(xiàng)互換時(shí),也可以使用以下代碼,但是效率很低,因?yàn)槊恳淮蔚腄om操作都將導(dǎo)致整個(gè)頁面的重新布局,所以不如直接修改元素的屬性值。

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

          function fnDown()
                  {
                      var sel = fnGetTarget("selLeftOrRight");
                      for(var i=sel.length -2; i >= 0; i--)
                      {//向下移動(dòng),最后一個(gè)不需要處理,所以直接從倒數(shù)第二個(gè)開始
                          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方法進(jìn)行操作,sort方法接受一個(gè)function參數(shù),可以在這個(gè)function里定義排序時(shí)使用的算法邏輯。
          array.sort([compareFunction]) 里compareFunction接受兩個(gè)參數(shù)(p1,p2),sort操作進(jìn)行時(shí),array對象會(huì)每次傳兩個(gè)值進(jìn)去,進(jìn)行比較;compareFunciton必須返回一個(gè)整數(shù)值:當(dāng)返回值>0時(shí),p1會(huì)排在p2后面;返回值<0時(shí),p1會(huì)排在p2前面;返回值=0時(shí),不進(jìn)行操作。
          例如:

          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的操作結(jié)果就是arr里的項(xiàng)按由小到大的升序排序
          //如果把fnCompare里改為
          //if (a < b)
          // return 1;
          //if (a > b)
          // return -1;
          //return 0;
          //則sort的結(jié)果是降序排列

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

          //因?yàn)榕判蚩梢园碠ption的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;
              }
              //將經(jīng)過排序的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;
          }
          排序時(shí)還可以有更多選項(xiàng),比如將value值看做Integer或是String進(jìn)行排序,得到的結(jié)果是不一樣的。篇幅限制,不在多做介紹。
          我將這些所有的操作都寫在了一個(gè)文件里,運(yùn)行的效果如圖(點(diǎn)擊看大圖)


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

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

          評論

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

          主站蜘蛛池模板: 灵石县| 兴海县| 安远县| 合山市| 嘉峪关市| 吉木乃县| 筠连县| 衢州市| 平利县| 卫辉市| 靖州| 沛县| 改则县| 台北市| 教育| 塔河县| 新安县| 三原县| 涪陵区| 兴宁市| 醴陵市| 惠东县| 金坛市| 吉木乃县| 佛坪县| 新郑市| 开鲁县| 陇川县| 瑞金市| 依兰县| 吴堡县| 建昌县| 沁源县| 开平市| 承德县| 芜湖县| 海晏县| 江城| 右玉县| 诸暨市| 武陟县|