用 jquery 操作 radio單選按鈕

2021-08-03 05:44:29 字數 2696 閱讀 5739

隨著jquery的作用越來越大,使用的朋友也越來越多。在web中,由於checkbox、radiobutton 、dropdownlist等控制項使用的頻率比較高,就關係到這些控制項在jquery中的操作問題。由於jquery的版本更新很快,**的寫法也改變了許多,以下jquery**適query1.4版本以上。

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

$(

'input:radio:checked').val();

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

// 個人推薦使用這一種

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

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');

4.根據索引值設定任意乙個radio為選中值:

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

...

或者

$(

'input:radio').slice(1,2).attr('checked', 'true');

5.根據value值設定radio為選中值

$(

"input:radio[value='rd2']").attr('checked','true');

或者$(

"input[value='rd2']").attr('checked','true');

6.刪除value值為rd2的radio

$(

"input:radio[value='rd2']").remove();

7.刪除第幾個radio

$("input:radio").eq(索引值).remove();索引值=0,1,2.

...

如刪除第3個radio:$(「input:radio」).eq(2).remove();

8.遍歷radio

獲取選中項:

獲取選中項的value值:

$('select#sel option:selected').val();

或者$('

select#sel').find('

option:selected').val();

獲取選中項的text值:

$('select#seloption:selected').text();

或者 $('select#sel').find('option:selected').text();

獲取當前選中項的索引值:

$(

'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;

「`設定最後乙個option為選中值:

使用JQUERY操作Radio

發展中經常使用radio為了實現使用者的選擇的影響。我在該專案中使用的一些jquery操作radio該方法。這裡分享,對於有需要的朋友參考的。1 變化radio選擇。引發一些結果 input radio name dialcheckresult change function 2 讓頁面中全部的ra...

利用jquery操作Radio方法小結

在開發中經常會用到radio來實現使用者的選擇效果,我在專案中積累了一些利用jquery來操作radio的方法,在這裡分享一下,供有需要的朋友借鑑。ps 移除diabled屬性 areaselect removeattr disabled 複製 如下 input radio name dialche...

jQuery操作單選按鈕 radio 用法

1.獲取選中值,四種方法都可以 input radio checked val input type radio checked val input name rd checked val var value input name radioname checked val 獲取被選中radio的v...