【发布时间】:2010-11-22 01:44:58
【问题描述】:
我正在寻找编写 jQuery 来仅启用单选按钮,具体取决于当前根据某些业务逻辑选择了哪些单选按钮。
基本上有 3 组 3 个单选按钮,最终看起来像(我很抱歉这个示例 HTML 很冗长,但希望这能说明我的意思):
<p>
<label for="group_one">Group One</label>
</p>
<p>
<div class="group_one">
<div id="group_one_choice_one">
<label for="group_one_choice_one">Choice One</label>
<br />
<input checked="checked" id="choice_one" type="radio" value="1" />
<br />
</div>
<div id="group_one_choice_two">
<label for="group_one_choice_two">Choice Two</label>
<br />
<input id="group_one_choice_two" type="radio" value="2" />
<br />
</div>
<div id="group_one_choice_three">
<label for="group_one_choice_three">Choice Three</label>
<br />
<input id="choice_three" type="radio" value="3"/ >
<br />
</div>
</div>
</p>
<p>
<label for="group_two">Group Two</label>
</p>
<p>
<div class="group_two">
<div id="group_two_choice_one">
<label for="group_two_choice_one">Choice One</label>
<br />
<input checked="checked" id="choice_one" type="radio" value="1" />
<br />
</div>
<div id="group_two_choice_two">
<label for="group_two_choice_two">Choice Two</label>
<br />
<input id="group_two_choice_two" type="radio" value="2" />
<br />
</div>
<div id="group_two_choice_three">
<label for="group_two_choice_three">Choice Three</label>
<br />
<input id="choice_three" type="radio" value="3"/ >
<br />
</div>
</div>
</p>
<p>
<label for="group_three">Group Three</label>
</p>
<p>
<div class="group_three">
<div id="group_three_choice_one">
<label for="group_three_choice_one">Choice One</label>
<br />
<input checked="checked" id="choice_one" type="radio" value="1" />
<br />
</div>
<div id="group_three_choice_two">
<label for="group_three_choice_two">Choice Two</label>
<br />
<input id="group_three_choice_two" type="radio" value="2" />
<br />
</div>
<div id="group_three_choice_three">
<label for="group_three_choice_three">Choice Three</label>
<br />
<input id="choice_three" type="radio" value="3"/ >
<br />
</div>
</div>
</p>
棘手的地方在于确定显示哪些单选按钮以及启用哪些单选按钮所需的逻辑。用户必须从每个组中选择一个选项,但不能从一个组到另一个组重复相同的选择。
理想情况下,当用户访问该页面时,所有单选按钮都可用但未被选中。
如果用户首先选择(例如)第三组中的选项二,则脚本应禁用第一组和第二组中的选项二。如果用户随后选择了第二组中的选项三,那么它应该只启用第一组中的选项一,依此类推。
任何帮助都将非常非常感谢。我会发布我一直在尝试编写的 jQuery 来解决这个问题,但我认为我一直没有很好地解决这个问题,并且希望得到任何帮助来解决这个问题。谢谢!
【问题讨论】:
-
这种方法的问题(?)是,一旦我做出选择,我就无法改变主意,因为所有按钮都被禁用。所以至少添加一个清除按钮。
-
你确定单选按钮是这里的答案,而不是复选框
标签: javascript jquery radio-button business-logic