首頁  >  文章  >  web前端  >  jquery對單選框,多選框,文字方塊等常見操作小結_javascript技巧

jquery對單選框,多選框,文字方塊等常見操作小結_javascript技巧

WBOY
WBOY原創
2016-05-16 17:04:511236瀏覽

一、文本框、單選按鈕、複選框、相關操作

複製代碼 程式碼如下:

var sex=$("input[name='sex']:checked").val();   //取得一組radio被選取項的值 
var item=$("#sel option:selected").text();      //取得select被選取項目的文字 
var option_num=$('#sel').val(); //取得select項目索引
$("#sel")[0].selectedIndex =1;                  //select找到下拉方塊的第二個元素為目前選取值
$$ ']").get(1).checked=true;     //radio單選組的第二個元素為目前選取值


 或預設單選框預設:
$("input[name='sex']").each(function(){
            if($(this).val()= =s){
                $(this).attr("checked",true);
        //this.checked=true;
            }
        });



Jquery 根據value值設置下拉列表(select)默認選中項 代碼如下:


 
  <script><BR>  function bao(s)<BR>  <script><BR>  function bao(s)<BR>  {<BR>     txt.value=s;<BR>     //選擇後,讓第一項被選中,這樣,就有Change啦.<BR>   true;<BR> }</script>
 
 
二、 jQuerySelect選擇的Text和取得Select選擇的Text Value
複製程式碼

程式碼如下:



程式碼如下:

語法解釋: $("#select_id").change(function(){//code...});   //為Select新增事件,當選擇其中一項時觸發 var checkText=$("#select_id" ).find("option:selected").text();  //取得Select選擇的Text
 var checkValue=$("#select_id").val();  //取得Select選擇的Value
var checkIndex=$("#select_id ").get(0).selectedIndex;  //取得Select選擇的索引值
 var maxIndex=$("#select_id option:last").attr("index"); //取得Select最大的索引值

jQuery設定Select選擇的Text和Value:

語法解釋:
 $("#select_id ").get(0).selectedIndex=1;  //設定Select索引值為1的項目選取
 $("#select_id ").val(4);   //設定Select的Value值為4的項目選取
 $("#select_id option[text='jQuery']" ).attr("selected", true);   //設定Select的Text值為jQuery的項目選取


jQuery新增/刪除Select的Option項目:

語法解釋:
 $("#select_id").append("");  //為Select追加一個Option(下拉項目)

 $("#select_id").prepend("");  //為Select插入一個Option(第一個位置) $("#select_id option:last").remove();  //刪除Select中索引值最大Option(最後一個) $("#select_id option[ index='0']").remove();  //刪除Select中索引值為0的Option(第一個) $("#select_id option[value='3']").remove( );  //刪除Select中Value='3'的Option $("#select_id option[text='4']").remove();  //刪除Select中Text='4'的Option
應用:
複製程式碼



複製程式碼







jQuery common




陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn