---
title: "(全选)多选按钮的JS实现 单选按钮"
author: "duyunfengsolo"
date: "2009-11-13T10:53:54Z"
tags: []
slug: "js"
draft: false
lastmod: "2009-11-13T11:04:45Z"
id: 516029
url: "https://solodu.iteye.com/blog/516029"
---

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>
```