html中<radio>单选按钮控件标签用法解析及如何设置默认选中

html-css08

html中<radio>单选按钮控件标签用法解析及如何设置默认选中,第1张

单选按钮是表示一组互斥选项按钮中的一个。当一个按钮被选中,之前选中的按钮就变为非选中的。

当单选按钮被选中或不选中时,该按钮就会触发 onclick 事件句柄。可通过遍历表单的 elements[] 数组来访问 Radio 对象,或者通过使用 document.getElementById()。

excel2010控件单选按钮的用法:

1、 在“开发工具”选项卡的“控件”组中选择“插入→选项按钮(窗体控件)”。

2、这时鼠标指针会变成细十字形,在工作表需要放置选项按钮的位置单击或拖动,即可绘制一个选项按钮。用同样的方法绘制多个选项按钮。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>New Web Project</title>

<script src="jquery-1.7.1.min.js"></script>

<script type="text/javascript">

window.onload= function(){

var inpt= document.getElementById('myForm').getElementsByTagName('input') //获取表单下所有的input元素

for(var i=0i<inpt.lengthi++){ //遍历获得的input元素

if(inpt[i].type=='radio'){ //如果是单选按钮

if(inpt[i].defaultChecked) //页面载入时选中的值

document.getElementById('text1').value=inpt[i].nextSibling.nodeValue//显示页面载入时选中的值

inpt[i].onclick=function(){ // input的单击事件

if(this.checked)

document.getElementById('text2').value=this.nextSibling.nodeValue//显示被选中的值

}

}

}

}

// 下面是用jquery实现

$(document).ready(function(){

$('input:radio').each(function(){

if(this.checked)

$('#text1').val($(this).val())

$(this).click(function(){

if(this.checked)

$('#text2').val($(this).val())

})

})

})

</script>

</head>

<body>

<form id="myForm">

<input type="radio" name="rad" checked="checked" value="音乐"/>音乐<br />

<input type="radio" name="rad" value="美术"/>美术<br />

<input type="radio" name="rad" value="电影"/>电影<br />

默认值:<input type="text" id="text1" /><br />

选中值:<input type="text" id="text2" />

</form>

</body>

</html>