2012-06-04 91 views
0

我一直在尝试所有周末来解决这个问题,我可能会过度复杂。下面是我正在工作的简化代码。任何帮助将不胜感激 - 因为我现在正在绞尽脑汁。动态检查两个复选框,使用javascript选择一个单选按钮

问题:检查两个复选框时,将选中相应的单选按钮。所以选择Earth和Wind复选框将选择'Earth and Wind'单选按钮。

<input name="single" type="checkbox" id="Earth"/>Earth<br> 
<input name="single" type="checkbox" id="Wind"/>Wind<br> 
<input name="single" type="checkbox" id="Emotion"/>Emotion<br> 
<input name="single" type="checkbox" id="Grass"/>Grass<br> 
<input name="single" type="checkbox" id="Good"/>Good<br> 

<hr> 

<input name="pair" type="radio" class="Earth Wind"/>Earth and Wind<br> 
<input name="pair" type="radio" class="Earth Emotion"/>Earth and Emotion<br> 
<input name="pair" type="radio" class="Earth Grass"/>Earth and Grass<br> 
<input name="pair" type="radio" class="Earth Good"/>Earth and Good<br> 
<input name="pair" type="radio" class="Wind Emotion"/>Wind and Emotion<br> 
<input name="pair" type="radio" class="Wind Grass"/>Wind and Grass<br> 
<input name="pair" type="radio" class="Wind Good"/>Wind and Good<br> 
<input name="pair" type="radio" class="Emotion Grass"/>Emotion and Grass<br> 
<input name="pair" type="radio" class="Emotion Good"/>Emotion and Good<br> 

编辑:要证据,我的努力:http://jsfiddle.net/rsF6w/:在那里我后两也已经禁用了复选框被选中。

回答

0

您将要将事件处理程序附加到onClick事件。在这些函数内部,您将抓取所有框的状态。在检查两个状态的情况下,您将检查相应的单选按钮。这是一个需要解决的问题。完整的代码会影响学习体验。

+0

谢谢[HTTP ://jsfiddle.net/rsF6w/](http://jsfiddle.net/rsF6w/)我有这么多,但我坚持。任何建议将非常有用谢谢 –

0

您可以给每个复选框使用位掩码的代码。然后做一个OR比较来得到单选按钮的位代码来选择。

阅读以了解更多信息: http://en.wikipedia.org/wiki/Mask_(computing

例如,地球将具有相应00000001代码,风力:,00000010,00000100情感等

地球风将得到代码:00000011

0

通过所有复选框(例如,例如)并获取检查哪两个。创建两个字符串,它们与所需单选按钮的名称相同(将它们在两个方向上连接起来)。这次通过单选按钮,如果您找到匹配的名称,请检查它。

确保你处理异常,例如同时选择> 2个复选框,不选择一个有效的对(你将不检查任何东西,还不如用布尔做到这一点)等

相关问题