<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    悟心

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

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

    獲取顯示的漢字

    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 艾波 閱讀(18981) 評論(1)  編輯  收藏 所屬分類: Javascript

    評論

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

    主站蜘蛛池模板: 精品久久久久久亚洲中文字幕| 亚洲一区二区三区四区视频| 老司机午夜免费视频| 永久免费AV无码网站在线观看| 亚洲二区在线视频| 曰曰鲁夜夜免费播放视频 | 91视频国产免费| 亚洲中文久久精品无码1| 青娱分类视频精品免费2| 亚洲三级在线视频| 四虎在线视频免费观看| 亚洲精品无码久久久久牙蜜区| 日本特黄a级高清免费大片| 久久精品亚洲日本波多野结衣| 国产乱子伦精品免费女| 另类小说亚洲色图| 久久精品国产亚洲5555| 日韩精品无码免费专区午夜| 久久丫精品国产亚洲av| 91成年人免费视频| 亚洲精品色播一区二区| 久久亚洲精品无码播放| 国产精品免费久久久久影院| 亚洲AV日韩AV一区二区三曲 | 又色又污又黄无遮挡的免费视| 久久亚洲国产伦理| 久久国产乱子伦免费精品| 亚洲av永久综合在线观看尤物| 国产男女猛烈无遮挡免费视频 | 四虎最新永久免费视频| 亚洲精品无码国产片| 亚洲乱码中文字幕手机在线| 久久成人无码国产免费播放| 亚洲av无码国产综合专区| 亚洲Av无码乱码在线观看性色| 在线观看片免费人成视频无码| 亚洲一区在线观看视频| 亚洲AⅤ无码一区二区三区在线| 亚洲免费观看视频| 亚洲精品无播放器在线播放| 国产日韩亚洲大尺度高清|