jquery - 根据选定的单选按钮显示/隐藏复选框
我目前正在为订单流程开发一个小表格。
该表单有多个字段,包括输入字段和单选框/复选框。
当前的形式如下:
<form id="order_process" name="order_process" method="post" action="">
<table>
<tr>
<td>
<label for="colour">Colour</label>
</td>
<td>
<label for="small">Small</label>
<input type="radio" name="type" value="Small" />
</td>
<td>
<input type="checkbox" name="colours" value="red" />
<input type="checkbox" name="colours" value="blue" />
<input type="checkbox" name="colours" value="yellow" />
</td>
<td>
<label for="medium">Medium</label>
<input type="radio" name="type" value="Medium" />
</td>
<td>
<input type="checkbox" name="colours" value="orange" />
<input type="checkbox" name="colours" value="blue" />
<input type="checkbox" name="colours" value="green" />
</td>
<td>
<label for="large">Large</label>
<input type="radio" name="type" value="Large" />
</td>
<td>
<input type="checkbox" name="colours" value="black" />
<input type="checkbox" name="colours" value="white" />
<input type="checkbox" name="colours" value="red" />
</td>
</tr>
</table>
</form>
我需要做的是,如果我选择特定的单选按钮,则仅显示颜色复选框。因此,这些复选框需要在页面加载时隐藏,并且仅根据所选的单选按钮显示。
我知道我可以使用 jQuery 来实现这一点,但我不确定如何做到这一点。
非常感谢
I am currently developing a small form for an order process.
The form has multiple fields, including input fields and radio/checkboxes.
The current form is as follows:
<form id="order_process" name="order_process" method="post" action="">
<table>
<tr>
<td>
<label for="colour">Colour</label>
</td>
<td>
<label for="small">Small</label>
<input type="radio" name="type" value="Small" />
</td>
<td>
<input type="checkbox" name="colours" value="red" />
<input type="checkbox" name="colours" value="blue" />
<input type="checkbox" name="colours" value="yellow" />
</td>
<td>
<label for="medium">Medium</label>
<input type="radio" name="type" value="Medium" />
</td>
<td>
<input type="checkbox" name="colours" value="orange" />
<input type="checkbox" name="colours" value="blue" />
<input type="checkbox" name="colours" value="green" />
</td>
<td>
<label for="large">Large</label>
<input type="radio" name="type" value="Large" />
</td>
<td>
<input type="checkbox" name="colours" value="black" />
<input type="checkbox" name="colours" value="white" />
<input type="checkbox" name="colours" value="red" />
</td>
</tr>
</table>
</form>
What I need to do, is only show the colours
checkboxes if i select a particular radio button. So these checkboxes need to be hidden on page load and only display based on the radio selected.
I know I can achieve this using jQuery, but I'm not sure how I'd do it.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先隐藏包含复选框的行,然后您所需要的就是:
这将显示与其关联的复选框行。它还将隐藏之前单击的其他单选按钮打开的任何行。
您可以在这里尝试一下:
http://jsfiddle.net/jXrxW/
First have the rows containing the checkboxes hidden and all you need thereafter is this:
This will show the row of checkboxes associated to it. It will also hide any previously open rows for a different radio button that had been clicked.
You can try it out here:
http://jsfiddle.net/jXrxW/
使用 jQuery 有很多方法可以做到这一点,但本质是您应该隐藏
$(document) 中的所有复选框 (
,然后将$('input type["checkbox"]')
) .readyclick
事件绑定到单选按钮 ($('input type["radio"]')
),这会显示相关项目。您可以通过向复选框添加 rel 值来指示哪个单选按钮应显示它们来简化此操作。There are many ways to do this with jQuery, but the essence is that you should hide all the checkboxes (
$('input type["checkbox"]')
) in$(document).ready
, and then bindclick
events to the radio buttons ($('input type["radio"]')
), which show the relevant items. You could make this easy by adding rel values to the checkboxes to indicate which radio button should show them.另一个解决方案是向复选框添加类,如下所示:
然后,在单选按钮上添加
change()
侦听器,并show()
相应的复选框:< br>
此处为 jsFiddle 示例
Another solution is to add classes to your checkboxes, like this :
And then, add a
change()
listener on your radio buttons, andshow()
the corresponding checkboxes :jsFiddle example here