jquery的checkbox,radio,select等方法總結

來源:互聯網
上載者:User

jquery的checkbox,radio,select等方法總結
jquery的checkbox,radio,select等方法總結,該文章為我的新部落格文章,以後此部落格更新頻率會降低,想查看我的最新部落格文章請看 http://www.haorooms.com/

jquery的checkbox,radio,和select是jquery操作的一個痛點和重點,很多前端新手對其瞭解不是很透徹。時間久了不用,我在寫的時候有時也難免對某些操作支支吾吾,記不清楚,現在,對其做一些簡單的總結!

1、checkbox日常jquery操作。 現在我們以下面的html為例進行checkbox的操作。

全選        項1        項2        項3        項4

全選和全部選代碼:

<script type="text/javascript">        $(function() {           $("#checkAll").click(function() {                $('input[name="subBox"]').attr("checked",this.checked);             });            var $subBox = $("input[name='subBox']");            $subBox.click(function(){                $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);            });        });    </script>

checkbox屬性:

var val = $("#checkAll").val();// 擷取指定id的複選框的值  var isSelected = $("#checkAll").attr("checked"); // 判斷id=checkAll的那個複選框是否處於選中狀態,選中則isSelected=true;否則isSelected=false;  $("#checkAll").attr("checked", true);// or  $("#checkAll").attr("checked", 'checked');// 將id=checkbox_id3的那個複選框選中,即打勾  $("#checkAll").attr("checked", false);// or  $("#checkAll").attr("checked", '');// 將id=checkbox_id3的那個複選框不選中,即不打勾  $("input[name=subBox][value=3]").attr("checked", 'checked');// 將name=subBox, value=3 的那個複選框選中,即打勾  $("input[name=subBox][value=3]").attr("checked", '');// 將name=subBox, value=3 的那個複選框不選中,即不打勾  $("input[type=checkbox][name=subBox]").get(2).checked = true;// 設定index = 2,即第三項為選中狀態  $("input[type=checkbox]:checked").each(function(){ //由於複選框一般選中的是多個,所以可以迴圈輸出選中的值      alert($(this).val());  }); 

2、radio的jquery日常操作及屬性 我們仍然以下面的html為例:

1  2  3  4 

radio操作如下:

$("input[name=radio]:eq(0)").attr("checked",'checked'); //這樣就是第一個選中咯。  //jquery中,radio的選中與否和checkbox是一樣的。$("#radio1").attr("checked","checked");$("#radio1").removeAttr("checked");$("input[type='radio'][name='radio']:checked").length == 0 ? "沒有任何單選框被選中" : "已經有選中";  $('input[type="radio"][name="radio"]:checked').val(); // 擷取一組radio被選中項的值  $("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");// 設定value = 2的一項為選中  $("#radio2").attr("checked", "checked"); // 設定id=radio2的一項為選中  $("input[type='radio'][name='radio']").get(1).checked = true; // 設定index = 1,即第二項為當前選中  var isChecked = $("#radio2").attr("checked");// id=radio2的一項處於選中狀態則isChecked = true, 否則isChecked = false;  var isChecked = $("input[type='radio'][name='radio'][value='2']").attr("checked");// value=2的一項處於選中狀態則isChecked = true, 否則isChecked = false;  

3、select下拉框的日常jquery操作 select操作相比checkbox和radio要相對麻煩一些,我們仍然以下面的html為例來說明:

      11      22      33      44      55      66   

看select的如下屬性:

    $("#select_id").change(function(){                         // 1.為Select添加事件,當選擇其中一項時觸發           //code...      });      var checkValue = $("#select_id").val();                    // 2.擷取Select選中項的Value      var checkText = $("#select_id :selected").text();          // 3.擷取Select選中項的Text       var checkIndex = $("#select_id").attr("selectedIndex");    // 4.擷取Select選中項的索引值,或者:$("#select_id").get(0).selectedIndex;      var maxIndex = $("#select_id :last").attr("index");        // 5.擷取Select最大的索引值,或者:$("#select_id :last").get(0).index;      /**      * jQuery設定Select的選中項      */      $("#select_id").get(0).selectedIndex = 1;                  // 1.設定Select索引值為1的項選中      $("#select_id").val(4);                                    // 2.設定Select的Value值為4的項選中      /**      * jQuery添加/刪除Select的Option項      */      $("#select_id").append("新增option");    // 1.為Select追加一個Option(下拉項)       $("#select_id").prepend("請選擇");   // 2.為Select插入一個Option(第一個位置)      $("#select_id").get(0).remove(1);                                      // 3.刪除Select中索引值為1的Option(第二個)      $("#select_id :last").remove();                                        // 4.刪除Select中索引值最大Option(最後一個)       $("#select_id [value='3']").remove();                                  // 5.刪除Select中Value='3'的Option       $("#select_id").empty();                $("#select_id").find("option:selected").text(); // 擷取select 選中的 text :   $("#select_id").val(); // 擷取select選中的 value:     $("#select_id").get(0).selectedIndex; // 擷取select選中的索引:     $("#select_id").get(0).selectedIndex=index;//index為索引值 //設定select 選中的value:    $("#select_id").attr("value","Normal“);    $("#select_id").val("Normal");    $("#select_id").get(0).value = value; //設定select 選中的text,通常可以在select回填中使用var numId=33 //設定text==33的選中!var count=$("#select_id  option").length;  for(var i=0;i

通過上面的總結,應該對jquery的checkbox,radio和select有了一定的瞭解了吧,溫故而知新,用多了就會變的熟練起來,即使有時候忘記了,也可以來翻一翻!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.