JQuery常用蓄積(三)Selectドロップダウンボックス

5327 ワード

<select id="hello"></select>

1.jQuery Selectで選択したTextとValueを取得
構文の説明:
 $("#select_id").change(function(){//code...});             // Select    ,           



var checkIndex=$("#select_id ").get(0).selectedIndex; //  Select      



var maxIndex=$("#select_id option:last").attr("index"); //  Select      



var checkValue=$("#select_id").val();                          //  Select   Value 



var firstText = $('#select_id option:first').val();            //      select  value 



var lastText = $('#select_id option:last').val();            //       select  value 



var secondText = $('#select_id option:eq(1)').val();      //      select  value 



var checkText=$("#select_id").find("option:selected").text(); //  Select   Text ,  ,   option:selected     first,last,eq(0),      



var length = $('#select_idoption').length;                  //  select   

2.jQuery設定Select選択のTextとValue
構文の説明:
1. $("#select_id ").get(0).selectedIndex=1;                         //  Select    1     

2. $("#select_id ").val(4);                                                  //  Select Value  4     

3. $("#select_id option[text='jQuery']").attr("selected", true); //  Select Text  jQuery     

3.jQuery SelectのOptionアイテムの追加/削除
構文の説明:
1. $("#select_id").append("<option value='Value'>Text</option>");    // Select    Option(   ) 

2. $("#select_id").prepend("<option value='0'>   </option>");     // Select      Option(     ) 

3. $("#select_id option:last").remove();                                        //  Select      Option(    ) 

4. $("#select_id option[index='0']").remove();                            //  Select     0 Option(   ) 

5. $("#select_id option[value='3']").remove();                          //  Select Value='3' Option 

5. $("#select_id option[text='4']").remove();                         //  Select Text='4' Option 

4.optionの遍歴と追加、削除
function changeShipMethod(shipping) {

    var len = $("select[@name=ISHIPTYPE] option").length

    if (shipping.value != "CA") {

        $("select[@name=ISHIPTYPE] option").each(function() {

            if ($(this).val() == 111) {

                $(this).remove();

            }

        });

    } else {

        $("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));

    }

}