ITeye博客

(全选)多选按钮的JS实现 单选按钮

duyunfengsolo · 2009-11-13 10:53 · JavaScript · Markdown

1.代码:(全选)多选按钮

<html>
  <head>
  	<title>复选框checked属性</title>
  	<script language="JavaScript" type="text/javascript">
  		<!--
  		    function myFunction()
  		    {
  		       var bBoolean = document.myForm2.cb.checked;
  		       if(bBoolean){
  		          document.myForm1.cb1.checked = true;
  		          document.myForm1.cb2.checked = true;
  		          document.myForm1.cb3.checked = true;
  		       }else{
  		          document.myForm1.cb1.checked = false;
  		          document.myForm1.cb2.checked = false;
  		          document.myForm1.cb3.checked = false;
  		       }
  		    }
   		//-->
  	</script>
  </head>
  <body>
  	<h1>请选择你的爱好</h1>
  	<form name="myForm1">
  		<input type="checkbox" name="cb1" checked>看书<br>
  		<input type="checkbox" name="cb2" checked>上网<br>
  		<input type="checkbox" name="cb3">游戏<br>
  	</form>
  	<hr>
  	<form name="myForm2">
  		<input type="checkbox" name="cb" onclick="myFunction()">全选
  	</form>
  </body>
</html>

 

2.单选按钮

<html>
  <head>
  	<title>单选按钮</title>
  	<script language="JavaScript" type="text/javascript">
  		<!--
  		    function myFunction1()
  		    {
  		       document.myForm1.rd[0].checked=true;
  		    }
  		    function myFunction2()
  		    {
  		       document.myForm1.rd[1].checked=true;
  		    }
  		    function myFunction3()
  		    {
  		       document.myForm1.rd[2].checked=true;
  		    }
   		//-->
  	</script>
  </head>
  <body>
  	<h1>请选择你最喜欢的爱好</h1>
  	<form name="myForm1">
  		<input type="radio" name="rd" checked>看书<br>
  		<input type="radio" name="rd">上网<br>
  		<input type="radio" name="rd">游戏<br>
  	</form>
  	<form name="myForm2">
  		<input type="button" value="选择看书" onclick="myFunction1()">
  		<input type="button" value="选择上网" onclick="myFunction2()">
  		<input type="button" value="选择游戏" onclick="myFunction3()">
  	</form>
  </body>
</html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。