Toggle div based on checkbox value

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I'm not sure how to accomplish this. What I want to do is to hide a div based on a checkbox value. This is my code for the toggle.

appreciate any help

.always is the checkbox

$(document).ready(function () {             $('.always').click(function () {                                 $('#dates').toggle();             });         }); 

回答1:

.toggle() takes a boolean as well, like this:

$(function () {   $('.always').change(function () {                      $('#dates').toggle(!this.checked);   }).change(); //ensure visible state matches initially }); 

You can test it out here. I assume in the above you want the #dates hidden if .always is checked, that's what it'll do.



回答2:

$('.always').change(function() {  $('#dates').toggle(!this.checked); }); 


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