如何使单选按钮只能为每个组选择1个选项?

时间:2016-02-25 13:01:29

标签: javascript html radio-button radiobuttonlist

我有5个单选按钮组。我希望用户只能在每个广播组中选择一个部分。我的第一组工作正常,只选择一个部分,但是当我选择某个部分而不是从1选择的所选按钮时,它选择另一个部分。

我的代码是:

<html>
<head>
<title> Checkboxes and radio buttons assignment 4 </title>
</head>
<body>

<form name=form1>

<h2>Complete the following</h2>
<hr>
Tick the box if you have the following:
<h2>History of Hereditary Disease </h2>
Heart <input type="checkbox" name=chkHeart value="Heart"> <br><br>
Asthma <input type="checkbox" name=chkAsthma value="Asthma"> 

<br><br>
Arthritis <input type="checkbox" name=chkArthritis 

value="Arthritis">
<br>
<hr>

Select the button that describes you:

<h2>Exercise</h2>
<input type="radio" name=radspeed CHECKED onclick="return 

Frequently_onclick(0)" value="Frequently"> Frequently
<input type="radio" name=radspeed onclick="return Average_onclick(1)" value="Average"> Average
<input type="radio" name=radspeed onclick="return Seldom_onclick(2)" value="Seldom"> Seldom
<br>
<hr>

<h2>Diet</h2>
<input type="radio" name=Low Fat CHECKED onclick="return LowFat_onclick(0)" value="Low Fat"> Low Fat
<input type="radio" name=Average onclick="return AverageFat_onclick(1)" value="Average"> Average
<input type="radio" name=High Fat onclick="return HighFat_onclick(2)" value="High Fat"> High Fat
<br>
<hr>

<h2>Alcohol</h2>
<input type="radio" name=LightAlcohol CHECKED onclick="return LightAlcohol_onclick(0)" value="Light"> Light
<input type="radio" name=MediumAlcohol onclick="return MediumAlcohol_onclick(1)" value="Medium"> Medium
<input type="radio" name=HeavyAlcholo onclick="return HeavyAlcohol_onclick(2)" value="Heavy"> Heavy
<br>
<hr>

<h2>Smoking</h2>
<input type="radio" name=NonSmoker CHECKED onclick="return NonSmoker_onclick(0)" value="Non-Smoker"> Non-Smoker
<input type="radio" name=LightSmoker onclick="return LightSmoker_onclick(1)" value="Light"> Light
<input type="radio" name=HeavySmoker onclick="return HeavySmoker_onclick(2)" value="Heavy"> Heavy
<br>
<hr>

<h2>General Health</h2>
<input type="radio" name=GoodHealth CHECKED onclick="return GoodHealth_onclick(0)" value="Good"> Good
<input type="radio" name=AverageHealth onclick="return AverageHealth_onclick(1)" value="Average"> Average
<input type="radio" name=PoorHealth onclick="return PoorHealth_onclick(2)" value="Poor"> Poor
<br>
<br>
<hr>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

您必须为特定组设置相同的名称 您的第一组具有相同的名称,另一组具有不同的名称 您可以查看Here