Uncheck or turn off all checkbox based toggle switches when a new one is turned on?

南笙酒味 提交于 2019-12-13 07:01:55

问题


I am using some CSS3 toggle switches in a project of mine and they are currently toggling just fine, but what i would like to have happen is for all other toggles to turn off when a new one is activated. Can anyone help get me started on this. I am not even sure where to start. These are the toggles I am using:

http://wsnippets.com/styling-checkbox-toggle-switches-css3/

is there a way for Javascript to be able to do this? Any guidance would be helpful.


回答1:


If you're using jQuery, this will do the trick:

$(function() {
  $('.checkbox-switch input').change(function() {
    if($(this).prop('checked')) {
      $(".checkbox-switch input").prop('checked', false);
      $(this).prop('checked', true);
    }
  });
});

See the demo using the example html/css in your link here: http://codepen.io/anon/pen/MymGqP.




回答2:


Without knowing too much about your project, one solution could be to use radio buttons instead of checkboxes for your toggle switches. That way, the default behaviour of the radio buttons will deselect the other/s when a new one is activated.



来源:https://stackoverflow.com/questions/36192286/uncheck-or-turn-off-all-checkbox-based-toggle-switches-when-a-new-one-is-turned

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