当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript radio值获取代码

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript radio值获取代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 366 ::
收藏到网摘: n/a

好多情况下,我们需要用js获取radio的值,下面是具体的实现方法,大家可以测试下。 [html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 获取 radio 选中值</title>
<script language="javascript" type="text/javascript">
//Copyright 2008
//By 小吴同学 QQ:100052800
//Date 2008-11-25
//$写法,用于获取元素ID名称
function $(id){return document.getElementById(id);}
//获取 radio 选中值
function GetValue(){
//获取 radio 所在容器名,根据容器名称搜索 radio 的标签 input
var radios = $("radios").getElementsByTagName("input");
//初始化对象
var grade = "";
//是否选中 radio,默认不选中,radio 可指定 checked="checked" 不受 flag 影响
var flag = false;
//根据容器中搜索到的 input 标签的个数(长度)进行遍历
for(var i=0;i<radios.length;i++){
//如果当前有选中
if(radios[i].checked == true){
//赋值
grade = radios[i].value;
//赋值
flag = true;
//结束循环
break;
}
}
//判断 radio 是否有选中
if(!flag){
alert("No choose");
return;
}else{
alert(grade);
}
}
</script>
</head>
<body style="font:12px 'Microsoft Sans Serif'">
<div id="radios">
<input name="radio" type="radio" value="1"/>1
<input name="radio" type="radio" value="2" />2
<input name="radio" type="radio" value="3" />3
<input name="radio" type="radio" value="4" />4
<input name="radio" type="radio" value="5" />5
<input name="button" type="button" value="GetValue" onclick="GetValue()"/>
</div>
</body>
</html>
[html]
js选中radio
document.consumerform.consumer_sex[0].checked=true;