我希望在选中另一个单选按钮时选中另一个单选按钮。这两个按钮有不同的名称。
当单选按钮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; ?>/>
希望我正确理解了你的问题。
如果你有这样的东西:
<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;
}
请注意,只有一个单选按钮可以选择相同的名称(如果他们包含不同的名称,就像你说的,然后它将工作)。在这种情况下,只有第二个将被选中,即使你点击了第一个。
你是说:这里有两个名字"sort"answers"animal"
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>