Radio buttons and checkboxes grouped together MVC

情到浓时终转凉″ 提交于 2019-12-25 08:26:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!