平常的工作中,我们有时候需要通过 <input type="radio" /> 来控制接下来模块的显示,今天我写了个例子,记录一下。
<!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 runat="server">
<title>test</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
</script>
</head>
<body>
<label>
<input name="type" type="radio" value="1" />
<span>页面抵达</span>
</label>
<label>
<input name="type" type="radio" value="2" />
<span>停留时长</span>
</label>
<label>
<input name="type" type="radio" value="3" />
<span>访问深度</span>
</label>
<label>
<input name="type" type="radio" value="4" />
<span>事件</span>
</label>
<br/><br/>
<div id="PageArrival">
<span>页面抵达 -- show</span>
</div>
<div id="Duration">
<span>停留时长 -- show</span>
</div>
<div id="Depth">
<span>访问深度 -- show</span>
</div>
<div id="Event">
<span>事件 -- show</span>
</div>
<script>
$(document).ready(function () {
$("input:radio[name='type']").change(
function () {
var blockArr = ["", "PageArrival", "Duration", "Depth", "Event"];
var selectedBlock = $('input:radio[name="type"]:checked').val();
for(var i=1; i<blockArr.length; i++){
if(selectedBlock != i){
$("#"+blockArr[i]).hide();
}else{
$("#"+blockArr[i]).show();
}
}
});
})
</script>
</body>
</html>
效果:
--------------------------------