使用jQuery检查复选框是否已在加载时选中 [英] Check if checkbox is ALREADY checked on load using jQuery

查看:74
本文介绍了使用jQuery检查复选框是否已在加载时选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用

 $("#some-box").click(function(){
     $("#main-box").toggle();
  });

这很好用,除了复选框是保存复选框状态的页面的一部分时.如果将复选框保存为选中状态,则主框(单击该复选框时会显示)在重新加载时隐藏,并且仅在单击该复选框时才可见,这在当前意味着未选中"复选框(如预期的那样)切换).

Which works well, except for when the checkbox is part of a page that saves the checkboxes status. If a checkbox is saved as ticked, the main-box (which is revealed when the checkbox is clicked) is hidden on reload, and is only visible if the checkbox is clicked, which in now would mean "unticked" checkbox (as expected with the toggle).

如果复选框已被选中,如何检查页面加载,在这种情况下如何自动触发切换?

How can I check on page load if the checkbox is already ticked, in which case to trigger the toggle automatically?

推荐答案

这应该有效:

if ($("#some-box").is(':checked')){
    $("#main-box").show();
}

这篇关于使用jQuery检查复选框是否已在加载时选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆