如何将单选按钮与另一个具有不同名称的单选按钮相关联

时间:2016-09-04 12:36:20

标签: php html forms button radio

我希望在检查另一台收音机后检查另一个单选按钮。这两个按钮有不同的名称。

现在选中单选按钮A,也应检查B。

<input type="hidden" name="menu_date[<?php $date_name ?>]" value="<?php echo $result['date']; ?>" />

<input type="radio" name="Menus[<?php echo $random_name; ?>]" value="<?php echo $result['foodMenuID']; ?>" id="Menus_1" for="menu_date[<?php $date_name ?>]" <?php echo $radio_state; ?>/>

2 个答案:

答案 0 :(得分:0)

我希望我能正确理解你的问题。

如果你有这样的事情:

<input type="radio" name="Menus[<?php echo $random_name; ?>]" value="<?php echo $result['foodMenuID']; ?>" id="Menus_1" for="menu_date[<?php $date_name ?>]" <?php echo $radio_state; ?>>
<input type="radio" name="Menus[<?php echo $random_name; ?>]" value="<?php echo $result['foodMenuID']; ?>" id="Menus_2" for="menu_date[<?php $date_name ?>]" <?php echo $radio_state; ?>>

然后将onclick事件添加到第一个单选按钮:

<input type="radio" name="Menus[<?php echo $random_name; ?>]" value="<?php echo $result['foodMenuID']; ?>" id="Menus_1" for="menu_date[<?php $date_name ?>]" <?php echo $radio_state; ?> onclick="markOther()">

然后编写一个非常简单的JavaScript函数:

function markOther()
{
    document.getElementById('Menus_2').checked = true;
}

请注意,只能选择1个具有相同名称的单选按钮(如果它们包含不同的名称,就像你说的那样,它会起作用)。在这种情况下,即使您单击第一个,也只会选择第二个。

答案 1 :(得分:0)

你的意思是: 这里有两个名字“排序”和“动物”

document.getElementById("herb").onclick = function()
{
 if (document.getElementById("herb").checked) 
   {                                      
	document.getElementById("cow").checked="qq";	
   }
}

document.getElementById("meat").onclick = function()
{
  if (document.getElementById("meat").checked) 
   {
     document.getElementById("tiger").checked="qq";
   }
}
<h1> Choose your favorite animal</h1>
<form id="mainForm" name="mainForm">
    <input type="radio" name="sort" id="herb" />Herbivores </br>
    <input type="radio" name="sort" id="meat" />Carnivore </br> </br>

	
    <input type="radio" name="animal" id="cow"/>cow </br>
    <input type="radio" name="animal" id="duck" />duck </br>
	<input type="radio" name="animal" id="buffalo" />buffalo </br> </br>
	
	<input type="radio" name="animal" id="lion"/>Lion </br>
    <input type="radio" name="animal" id="croco" />Crocodile </br>
	<input type="radio" name="animal" id="tiger" />Tiger </br>

</form>

如果您想要任何修改,欢迎您。

相关问题