JQuery中_Radio、DropDownList、Checkbox選擇控件的處理

Radio  javascript

1.獲取選中值,三種方法均可以:java

$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val(); jquery

2.設置第一個Radio爲選中值:
$('input:radio:first').attr('checked', 'checked'); 或者
$('input:radio:first').attr('checked', 'true'); 注: attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)
3.設置最後一個Radio爲選中值: $('input:radio:last').attr('checked', 'checked');
              或者 $('input:radio:last').attr('checked', 'true'); app


4.根據索引值設置任意一個radio爲選中值: dom

$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2.... 或者 $('input:radio').slice(1,2).attr('checked', 'true');this

5.根據Value值設置Radio爲選中值
$("input:radio[value='rd2']").attr('checked','true'); 或者
$("input[value='rd2']").attr('checked','true'); spa

6.刪除Value值爲rd2的Radio
$("input:radio[value='rd2']").remove(); .net

7.刪除第幾個Radio
$("input:radio").eq(索引值).remove();索引值=0,1,2....
如刪除第3個Radio:$("input:radio").eq(2).remove();code

8.遍歷Radio
$('input:radio').each(function(index,domEle){ //寫入代碼 });
DropDownList blog

1. 獲取選中項: 獲取選中項的Value值: $('select#sel option:selected').val(); 或者
$('select#sel').find('option:selected').val(); 獲取選中項的Text值:
$('select#seloption:selected').text(); 或者
$('select#sel').find('option:selected').text();

2. 獲取當前選中項的索引值:
$('select#sel').get(0).selectedIndex;

3. 獲取當前option的最大索引值:
$('select#sel option:last').attr("index")

4. 獲取DropdownList的長度:
$('select#sel')[0].options.length; 或者
$('select#sel').get(0).options.length;

5. 設置第一個option爲選中值:
$('select#sel option:first').attr('selected','true') 或者
$('select#sel')[0].selectedIndex = 0;

6. 設置最後一個option爲選中值:
$('select#sel option:last).attr('selected','true')

7. 根據索引值設置任意一個option爲選中值: $('select#sel')[0].selectedIndex =索引值;索引值=0,1,2....


8. 設置Value=4 的option爲選中值: $('select#sel').attr('value','4'); 或者
$("select#sel option[value='4']").attr('selected', 'true');

9. 刪除Value=3的option: $("select#sel option[value='3']").remove();
10.刪除第幾個option: $(" select#sel option ").eq(索引值).remove();索引值=0,1,2....
如刪除第3個Radio: $(" select#sel option ").eq(2).remove();


11.刪除第一個option: $(" select#sel option ").eq(0).remove(); 或者
$("select#sel option:first").remove();

12. 刪除最後一個option:
$("select#sel option:last").remove();

13. 刪除dropdownlist:
$("select#sel").remove();

14.在select後面添加一個option:
$("select#sel").append("<option value='6'>f</option>");

15. 在select前面添加一個option: $("select#sel").prepend("<option value='0'>0</option>");

16. 遍歷option: $(' select#sel option ').each(function (index, domEle) { //寫入代碼 }); CheckBox

1. 獲取單個checkbox選中項(三種寫法):
$("input:checkbox:checked").val() 或者
$("input:[type='checkbox']:checked").val(); 或者
$("input:[name='ck']:checked").val();

2. 獲取多個checkbox選中項:
$('input:checkbox').each(function() { if ($(this).attr('checked') ==true) { alert($(this).val()); } });

3. 設置第一個checkbox 爲選中值:
$('input:checkbox:first').attr("checked",'checked'); 或者
$('input:checkbox').eq(0).attr("checked",'true');

4. 設置最後一個checkbox爲選中值:
$('input:radio:last').attr('checked', 'checked'); 或者
$('input:radio:last').attr('checked', 'true');

5. 根據索引值設置任意一個checkbox爲選中值: $('input:checkbox).eq(索引值).attr('checked', 'true');索引值=0,1,2.... 或者 $('input:radio').slice(1,2).attr('checked', 'true');

6. 選中多個checkbox: 同時選中第1個和第2個的checkbox:
$('input:radio').slice(0,2).attr('checked','true');

7. 根據Value值設置checkbox爲選中值:
$("input:checkbox[value='1']").attr('checked','true');

8. 刪除Value=1的checkbox: $("input:checkbox[value='1']").remove();

9. 刪除第幾個checkbox: $("input:checkbox").eq(索引值).remove();索引值=0,1,2....
如刪除第3個checkbox: $("input:checkbox").eq(2).remove();

10.遍歷checkbox:
$('input:checkbox').each(function (index, domEle) { //寫入代碼 });
11.所有選中 $('input:checkbox').each(function() {
$(this).attr('checked', true); });

12.所有取消選擇:
$('input:checkbox').each(function () { $(this).attr('checked',false); });

 ===========================如下代碼和上面大可能是重複的=============================

jQuery獲取Select選擇的Text和Value:

語法解釋:
1. $("#select_id").change(function(){//code...});   //爲Select添加事件,當選擇其中一項時觸發
2. var checkText=$("#select_id").find("option:selected").text();  //獲取Select選擇的Text

3. var checkValue=$("#select_id").val();  //獲取Select選擇的Value

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

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

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的項選中 
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 

6. $("#select_id option[text='4']").remove();  //刪除Select中Text='4'的Option


jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關 
獲取一組radio被選中項的值  var item = $('input[@name=items][@checked]').val(); 
獲取select被選中項的文本  var item = $("select[@name=items] option[@selected]").text(); 

select下拉框的第二個元素爲當前選中值 
$('#select_id')[0].selectedIndex = 1; 

 $("#select_id'").attr("value", '12'); //設置value=12的項目爲當前選中項
 $("select#select_id option:last").attr('selected', true);  //設置下拉列表中的最後一個值爲當前選中

radio單選組的第二個元素爲當前選中值 
$('input[@name=items]').get(1).checked = true; 
獲取值: 
  文本框,文本區域:$("#txt").attr("value"); 
多選框checkbox:$("#checkbox_id").attr("value"); 

單選組radio:   $("input[@type=radio][@checked]").val(); 

下拉框select: $('#sel').val(); 
控制表單元素: 
文本框,文本區域:$("#txt").attr("value",'');//清空內容                  

$("#txt").attr("value",'11');//填充內容 


多選框checkbox: $("#chk1").attr("checked",'');//不打勾                 

  $("#chk2").attr("checked",true);//打勾                  

if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾 
單選組radio:    $("input[@type=radio]").attr("checked",'2');//設置value=2的項目爲當前選中項 
下拉框select:   $("#sel").attr("value",'-sel3');//設置value=-sel3的項目爲當前選中項 
                $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option 
                $("#sel").empty();//清空下拉框

-------------------------------------------------------------------

//遍歷option和添加、移除

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]"));
}
}

 

//取得下拉選單的選取值 $(#testSelect option:selected').text(); 或$("#testSelect").find('option:selected').text(); 或$("#testSelect").val();

相關文章
相關標籤/搜索