
[英]Unable to select dynamically generated radio button programmatically using javascript
[英]Dynamically checking two checkboxes to select one radio button using javascript
我一直在整个周末努力解决这个问题,我可能使它过于复杂了。 下面是我正在工作的简化代码。 任何帮助将不胜感激-因为我已经为此花了很长时间才绞尽脑汁。
问题 :选中两个复选框后,相应的单选按钮被选中。 因此,选中“地球和风”复选框将选择“地球和风”单选按钮。
<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/ :在选中两个复选框后,我也禁用了复选框。
您将需要将事件处理程序附加到onClick
事件。 在这些功能内,您将获取包装盒的所有状态。 如果检查了两种状态,则将检查相应的单选按钮。 这是一个好问题要解决。 完整的代码会影响学习经验。
您可以为每个复选框提供一个使用位掩码的代码。 然后进行“或”比较,以选择单选按钮的位代码。
请阅读以下内容以获取更多信息: http : //en.wikipedia.org/wiki/Mask_(计算 )
例如,地球将具有与00000001,风:00000010,情感00000100等对应的代码。
地风将获得代码:00000011
通过所有复选框去( 与 ,例如),并得到其中两个被选中。 创建两个字符串,它们与所需的单选按钮的名称相同(将它们双向连接)。 这次通过单选按钮,如果找到匹配的名称,请检查它。
确保处理异常,例如一次选择> 2个复选框,而不选择有效的对(您不会检查任何内容,最好使用布尔值也可以),等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.