问题
I'm trying to find an MVC (razor) solution where I can use radio buttons and checkboxes together.
Say there are 2 radiobuttons
labelled: All people and Family Members.
If you select the "Family Members" radio button
, 3 checkboxes
should be enabled: "Employee", "Spouse", "Dependant(s)"
The questions are:
- How to group a radio button to 3 checkboxes?
- How to enable the checkboxes when the related radio button is selected?
回答1:
Well you could do the following:
<ul>
<li><input type="radio">All People</input></li>
<li>
<input type="radio" class="toggle-family">Family Members</input>
<ul class="member-selection hide">
<li><input type="checkbox">Employee</input></li>
...
</ul>
</li>
</ul>
Then in your javascript you could do the following:
$(function() {
$('.toggle-family').change(function() {
if ($(this).attr('checked')) {
$('.member-selection').removeClass('hide');
} else {
$('.member-selection').addClass('hide');
}
});
});
Then you would need to just define a css class called "hide" and set it display attribute to none. This is just an idea to get you started, hope it helps.
来源:https://stackoverflow.com/questions/8497629/radio-buttons-and-checkboxes-grouped-together-mvc