jQuery – 检查单选按钮的当前状态并隐藏div

问题:我如何检查单选按钮onload的值,如果它是某个值,请执行基于此的操作? 在if语句的代码末尾引用。

更深入的解释

所以我知道这个问题的标题可能听起来很熟悉其他问题,但我有更多的参与,并试图找到最好的方法来处理它。 因此,在我的函数homepageDisplayHandleSelection()我根据从.homepage_display区域中选择的单选按钮来隐藏/显示区域。 然后,我基本上将相同的function用于其中的另一组单选按钮,称为mainImageLinkHandleSelection ,然后根据选择隐藏/显示区域。 我可以在StaticSlide选项之间切换就好了,但是初始视图(如果页面加载时选择了Slideshow )是它仍然显示辅助单选按钮mainImageLinkHandleSelection ,直到我单击Slideshow选项并且回到Static ,它纠正自己。

所以,我尝试通过创建一个检查当前检查的无线电值是否是slideshow来检查这个(在示例代码的底部),如果是,我隐藏了一个名为main_link的div(它也附加到每个区域)围绕mainImageLinkHandleSelection divs)。 我知道我的问题有很多部分,但我想如果有人可以提供帮助,那里会有一个天才;)非常感谢你们所有的帮助!

 jQuery(document).ready(function() { // Homepage Settings - Homepage Display // This function handles the radio clicks. function homepageDisplayHandleSelection() { var duration = 400; jQuery('.homepage_display').hide(duration); jQuery('.main_link').hide(duration); if (this.value === "slideshow") { jQuery('.homepage_display_slides').show(duration); } else if (this.value === "static") { jQuery('.homepage_display_static').show(duration); } } // Attach a click handler to the radios // and trigger the selected radio jQuery('#section-of_homepage_display :radio') .click(homepageDisplayHandleSelection) .filter(':checked').trigger('click'); // Homepage Settings - Main Link Options // This function handles the radio clicks. function mainImageLinkHandleSelection() { var duration = 400; jQuery('.main_link').hide(duration); if (this.value === "external_url") { jQuery('.main_link_external').show(duration); } else if (this.value === "wp_page") { jQuery('.main_link_page').show(duration); } else if (this.value === "wp_post") { jQuery('.main_link_post').show(duration); } else if (this.value === "wp_taxonomy") { jQuery('.main_link_category').show(duration); } } // Attach a click handler to the radios // and trigger the selected radio jQuery("#section-of_main_image_link_option :radio") .click(mainImageLinkHandleSelection) .filter(":checked").trigger("click"); // Make sure main image link option are hidden by default if slideshow option is selected if (jQuery('#section-of_homepage_display :radio :checked').val() === 'slideshow') { jQuery('.main_link').hide(); } }); 

好吧,我假设每个单选按钮都有一个唯一的值,并且它们都被赋予相同的名称。 在这种情况下,你想要的是:checked选择器:

 var $selected = $('input[name="RadioGroup"]:checked', '#someform'); if($selected.length == 0) { // None is selected } else { var whichOne = $selected.val(); // do stuff here } 

这将获得当前所选单选按钮的值。 如果您已经拥有要检查的单选按钮的id ,那么它就像下面这样简单:

 $('#someradiobutton').is(':checked') // returns true if checked, else false 

具体来说,由于您特别想要检查slideshow值,我相信您的if语句将变为:

 if (jQuery('input[name="mainImageLinkHandleSelection"][value="display"]', '#section-of_homepage_display').is(':checked')) { jQuery('.main_link').hide(); } 

编辑 :我现在看到你的问题。 问题是您隐藏了所有.main_link元素以及.homepage_display元素。 但是,当您单击主页选择单选按钮时,您不会通过并重新显示由yhlumberyardstraditional3[of_main_image_link_option]单选按钮组反映的.main_link元素。

两件事情:

  1. 使用change事件处理程序而不是click 。 这将防止在单击已选择的单选按钮时不必要地运行整个动画序列。
  2. 显示元素时,您需要查看是否选中了任何单选按钮,然后触发这些单选按钮的change事件处理程序。

我已经用概念validation更新了你的小提琴 。 主要变化如下:

 var toShow = null; if (this.value === "slideshow") { toShow = '.homepage_display_slides'; } else if (this.value === "static") { toShow = '.homepage_display_static'; } if(toShow) { jQuery(toShow).show(duration); // If we just showed any checked radio buttons, let's re-trigger // them so they can update their groups and such jQuery('input:checked:visible', toShow).trigger('change'); } 

我也冒昧地将问题归纳并将代码缩减到39行。 看看这里 。