jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery – 检查单选按钮的当前状态并隐藏div大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@
问题:如何检查单选按钮的值是否为某个值,是否基于此执行操作?在if语句的代码末尾引用.

更深入的解释

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

所以,我尝试通过创建一个检查当前检查的无线电值是否是幻灯片显示来检查这个(在示例代码底部),如果是,我隐藏了一个名为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();
    }

});

解决方法

好吧,我假设每个单选按钮都有一个唯一的值,并且它们都被赋予相同的名称.@L_450_34@种情况下,你想要的是: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

具体来说,由于您特别想要检查幻灯片显示值,我相信您的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元素.

两件事情:

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

凭借概念验证,我已经走了@L_874_42@.主要变化如下:

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行.看看here.

@H_801_48@

大佬总结

以上是大佬教程为你收集整理的jQuery – 检查单选按钮的当前状态并隐藏div全部内容,希望文章能够帮你解决jQuery – 检查单选按钮的当前状态并隐藏div所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。