DIV CSS 佈局教程網

jQuery 取得 radio 值的實例教程
編輯:JQuery特效代碼     

本文示範用 jQuery 取得 radio 單選組值的方法,分享給大家參考。

取得選中項的值:

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

設置 value=2 的項目為當前選中項:

$("input[@type=radio]").attr("checked",'2');

取得一組 radio 被選中項的值:

var item = $('input[@name=items][@checked]').val();

讓 radio 單選組的第二個元素為當前選中值:

$('input[@name=items]').get(1).checked = true

注意寫法會因 jQuery 版本而異:

jQuery 舊版本:

var_name = $("input[@name='radio_name']:checked").val();

jQuery 1.3 以後的版本(移除 @ 符號):

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

實例:根據選中值顯示不同內容

function getra(){
var_name = $("input[name='isspecialcnt']:checked").val();
if(var_name=='1'){
$("#isspecialcntyes").show();
$("#isspecialcntno").hide();
}
if(var_name=='0'){
$("#isspecialcntyes").hide();
$("#isspecialcntno").show();
}
}

特別提醒:在 jQuery 1.3 之後,屬性選擇器不再使用 @ 前綴,寫成 input[name='xxx']:checked 才是正確語法,這是許多人容易踩的雷。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved