如何读取单选按钮被选中的值,如何设置单选按钮
2010-09-06 22:45
549 查看
在一组单选按钮中,他们的name属性相同,则这些单选按钮只有一个被选中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>单选项</title>
<script language="javascript">
//获得被选中的值
function getChoice(){
var oForm = document.forms["myForm1"];
var aChoices = oForm.camera;
//遍历整个单选项表
for(i=0;i<aChoices.length;i++){
//如果发现了被选中项则退出
if(aChoices[i].checked)
break;
}
document.getElementById("txtResult").innerHTML="您使用的相机品牌是:"+aChoices[i].value;
}
//设置某选项被选中
function setChoice(iNum){
var oForm = document.forms["myForm1"];
oForm.camera[iNum].checked = true;
}
</script>
</head>
<body>
<form method="post" name="myForm1">
您使用的相机品牌:<br />
<input type="radio" name="camera" id="canon" value="Canon">
<label for="canon">Canon</label><br />
<input type="radio" name="camera" id="nikon" value="Nikon">
<label for="nikon">Nikon</label><br />
<input type="radio" name="camera" id="sony" value="Sony" checked>
<label for="sony">Sony</label><br />
<input type="radio" name="camera" id="others" value="其它">
<label for="others">others</label><br />
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn">
<input type="button" value="检测选中对象" onclick="getChoice();">
<input type="button" value="设置为Canon" onclick="setChoice(0);">
<p><span id="txtResult"></span></p>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>单选项</title>
<script language="javascript">
//获得被选中的值
function getChoice(){
var oForm = document.forms["myForm1"];
var aChoices = oForm.camera;
//遍历整个单选项表
for(i=0;i<aChoices.length;i++){
//如果发现了被选中项则退出
if(aChoices[i].checked)
break;
}
document.getElementById("txtResult").innerHTML="您使用的相机品牌是:"+aChoices[i].value;
}
//设置某选项被选中
function setChoice(iNum){
var oForm = document.forms["myForm1"];
oForm.camera[iNum].checked = true;
}
</script>
</head>
<body>
<form method="post" name="myForm1">
您使用的相机品牌:<br />
<input type="radio" name="camera" id="canon" value="Canon">
<label for="canon">Canon</label><br />
<input type="radio" name="camera" id="nikon" value="Nikon">
<label for="nikon">Nikon</label><br />
<input type="radio" name="camera" id="sony" value="Sony" checked>
<label for="sony">Sony</label><br />
<input type="radio" name="camera" id="others" value="其它">
<label for="others">others</label><br />
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn">
<input type="button" value="检测选中对象" onclick="getChoice();">
<input type="button" value="设置为Canon" onclick="setChoice(0);">
<p><span id="txtResult"></span></p>
</form>
</body>
</html>
相关文章推荐
- 如何判断单选按钮radio被选中
- html中<radio>单选按钮控件标签用法解析及如何设置默认选中
- Easyui的datagrid中有分页,如何设置分页刷新按钮
- js拿到单选按钮的值,以及js动态设置form表单的action值
- android-如何在ListView中显示单选按钮并能默认选中
- 通过值设置单选按钮radio,多选按钮checkbox
- JS如何获取和改变 单选按钮radio选中的值
- 5. 在cocos2d里面如何制作按钮:简单按钮、单选按钮和开关按钮
- jquery循环遍历radio单选按钮,并设置选中状态
- 如何设置xml中的product的读取值
- qt 设置单选按钮(checkbox或radiobutton等)的方法
- Android开发_如何设置按钮背景透明与半透明_图片背景透明
- (译)在cocos2d里面如何制作按钮:简单按钮、单选按钮和开关按钮
- 如何设置约束让三个按钮平分UIView容器的宽度的方法
- Android如何设置圆角按钮
- Delphi 如何操作外部程序的控件(如按钮,文本框,单选按钮等)
- [转]Qt中使用GridLayout如何设置一个按钮占两个位置
- IOS 如何给自定义按钮设置选中的效果
- IIS7 如何设置读取、脚本和可执行文件的执行权限