开发者

Toggle element visibility based on checkbox state

开发者 https://www.devze.com 2023-01-28 05:39 出处:网络
I\'m not sure how to accomplish this. What I want to开发者_Go百科 do is to hide a div based on a checkbox value. This is my code for the toggle, .always is the checkbox and #dates is a div.

I'm not sure how to accomplish this. What I want to开发者_Go百科 do is to hide a div based on a checkbox value. This is my code for the toggle, .always is the checkbox and #dates is a div.

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


.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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号