jquery 操作select,checkbox,radio (整理)

來源:互聯網
上載者:User

標籤:select   checkbox   radio   操作   jquery操作   

在工作中經常用到select,checkbox,radio,今天有點空閑就整理一下,免得以後用的時候還要重新找。

操作select下拉框

—— 擷取值或選中項:

1, $("#select_id").change(function(){//code...});  //為Select添加事件,當選擇其中一項時觸發

2,var checkValue=$("#select_id").val(); //擷取Select選擇的Value

3,var checkText=$("#select_id").find("option:selected").text(); //擷取Select選擇的Text

4,$("#columnName").find("option:selected").attr("price");//擷取select選擇項中的price屬性的值

5,$("#columnName option[value=‘3‘]").attr("price");//擷取value值為3的選擇項的price的屬性的值

6,var checkIndex=$("#select_id ").get(0).selectedIndex; //擷取Select選擇的索引值

7,var maxIndex=$("#select_id option:last").attr("index"); //擷取Select最大的索引值

8,$("#select_id option[value=‘3‘]").attr("selected",true);//根據value值為3的項選中

9,$("#select_id ").val(‘4’);  // 設定Value值為4的項選中

10,$("#select_id ").get(0).selectedIndex=2; //設定Select索引值為2的項選中

11,$("#select_id option[text=‘jQuery‘]").attr("selected", true); //設定Select的Text值為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


操作checkbox多選框


1,//由於複選框一般選中的是多個,所以可以迴圈輸出選擇項的值

          var payFlightSts=‘‘;
          $(‘input[name="payFlightSts_‘+currCount+‘"]:checked‘).each(function(){
              payFlightSts+=$(this).val()+‘,’;
          });

2,//擷取未選中的checkbox的值:

    $("input[name=‘box‘]").each(function(){
          if ($(this).attr(‘checked‘) ==false) {
                alert($(this).val());
            }
     });

3,//全選
    $("#btn1").click(function(){
        $("input[name=‘box‘]").attr("checked","true");
    })
4,//取消全選
     $("#btn2").click(function(){
         $("input[name=‘box‘]").removeAttr("checked");
     })

5,//返選

$("#btn4").click(function(){
    $("input[name=‘checkbox‘]").each(function(){
         if($(this).attr("checked")) {
            $(this).removeAttr("checked");
         } else {
             $(this).attr("checked","true");
        }
    })
})


操作radio單選框

1,$(‘input[name="testradio"]:checked‘).val();//擷取選擇的value值

2,//遍曆name為testraio的所有選擇項

         $(‘input[name="testradio"]‘).each(function(){

alert(this.value);});





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.