jQuery toggle() issue

jQuery toggle() issue

本文关键字:issue toggle jQuery      更新时间:2023-09-26

我使用的是本次讨论中的脚本:https://wordpress.stackexchange.com/a/14711/14649

它工作得很好,但当我选择不同的无线电输入时,原始框不会关闭(坏),但新框会出现(好)。我添加的任何其他框都会发生这种情况。

jQuery不是我喜欢的,我一直在四处研究,但运气不好!这是我的代码:

<script type='text/javascript'>
 jQuery(document).ready(function($) {
        $('#feature_box').hide();
        $('#standard_lead').hide();             
        // one box
        $('#value_feature_box').is(':checked') ? $("#feature_box").show() : $("#feature_box").hide();
        $('#value_feature_box').click(function() {
            $("#feature_box").toggle(this.checked);
        });
        // second box
        $('#value_standard_lead').is(':checked') ? $("#standard_lead").show() : $("#standard_lead").hide();
        $('#value_standard_lead').click(function() {
            $("#standard_lead").toggle(this.checked);
        });
    });
</script>

谢谢你看!

另一个盒子之所以没有消失,是因为你没有告诉它!

$('#value_feature_box').click(function() {
    $("#feature_box").toggle(this.checked);
});

这表示在单击#value_feature_box时切换#feature_box的可见性。如果#value_feature_box不是复选框,则将内部代码更改为$("#feature_box").toggle();。如果你想让一些东西也消失,那么告诉它:

$('#value_feature_box').click(function() {
    $("#feature_box").toggle();
    $("#standard_lead").toggle();
});

toggle()括号内的值告诉它出现或消失(true或false)。

点击时只切换一个框-需要同时切换两个框,请参阅jsFiddle:

  • http://jsfiddle.net/Gr8Jq/

有了这个(对于另一个盒子也是类似的):

    $('#value_feature_box').click(function() {
        $("#feature_box").toggle(this.checked);
    });

您基本上是在说-单击时(因此this.checked将始终为true),切换元素。来自文档:

  • http://api.jquery.com/toggle/

您使用的toggle版本具有showOrHide参数,并且始终传递true,因此始终显示并仅显示单击的框。