【问题标题】:JQuery .hide()/.show() with .prop() setting of checkboxJQuery .hide()/.show() 与复选框的 .prop() 设置
【发布时间】:2012-06-19 01:31:12
【问题描述】:

这里是JS Fiddle。我希望内容具有一定的交互性,只需单击内容即可隐藏它。为了允许用户在他/她愿意的情况下将其带回,隐藏部分出现在右侧的<aside> 部分中。我有“标题”(业务方法)工作,因为它必须保留为父 DOM 元素。我一直在努力让孩子们工作。我目前对孩子们的尝试是/是这个(也张贴在小提琴上):

    //individual BA elements Toggle Buttons
    //Currently on, turning Off
        $('.block > .businessapproaches > section > input').on('change', function () {
            if (this.checked) {
            } else {
                var index = $(this).prevAll('section').length;
                $('.block > .businessapproaches > section').eq(index).hide();
                $('fieldset.businessapproaches > label').eq(index+1).show();
                $('fieldset.businessapproaches > input').eq(index+1).prop('checked' , false);
                console.log("off")
                console.log("left index: " + index);
                console.log($('.block > .businessapproaches > section').eq(index));
                console.log($('fieldset.businessapproaches > label').eq(index+1));
            }
        });
    //Currently off, turning On
        $('fieldset.businessapproaches > input').on('change', function () {
            if (this.checked) {
            } else {
                var index = $(this).prevAll('section').length+1;
                $('.block > .businessapproaches > section').eq(index-1).show();
                $('.block > .businessapproaches > input').eq(index-1).prop('checked' , false);
                $('fieldset.businessapproaches > label').eq(index).hide();
                console.log("on")
                console.log("right index: " + index);
                console.log($('.block > .businessapproaches > section').eq(index-1));
                console.log($('fieldset.businessapproaches > label').eq(index));
            }
        });

很抱歉在同一个堆栈中发布 2 个问题,但它似乎相关,我正在努力快速达到 125 的代表,因此我可以发布多个问题而无需等待规定的时间)

奖励积分:能告诉我为什么“标题”(业务方法)可以通过复选框或文本单击,而子项则不能?我的错误在哪里,因为我最终会用 css 隐藏“框”。

【问题讨论】:

    标签: javascript jquery html show-hide


    【解决方案1】:

    我试过小提琴http://fiddle.jshell.net/guanxiaohua2k6/dpvAn/1/。我认为它可以按您的意愿工作。

    我将在下面显示修改位置。

    @@ -10 +10 @@
    -                    var index = $(this).prevAll('section').length;
    +                    var index = $(this).closest('section').prevAll('section').length;
    @@ -13 +13 @@
    -                    $('fieldset.businessapproaches > input').eq(index+1).prop('checked' ,     false);
    +                    $('fieldset.businessapproaches > input').eq(index+1).prop('checked' , true);
    @@ -24 +24 @@
    -                    var index = $(this).prevAll('section').length+1;
    +                    var index = $(this).prevAll('input').length;
    @@ -26 +26 @@
    -                    $('.block > .businessapproaches > input').eq(index-1).prop('checked' , false);
    +                    $('.block > .businessapproaches > section > input').eq(index-1).prop('checked' , true);
    

    至于为什么不能点击children的'header',输入的id和

    【讨论】:

    • 1:你能跟进一个更好的解释吗,因为我不知道为什么你必须添加 .closest() 函数,以及当父母是假,然后真。 2:哦,嗯!,这就是我复制动态生成的 HTML 所得到的!,感谢您的关注。
    • 根据 jQuery 的描述“获取匹配元素集中每个元素的所有前面的兄弟姐妹,可选地通过选择器过滤。”,prevAll 只获取前面的兄弟姐妹。因此,如果您想获取之前的部分,您应该首先使用最接近()获取当前部分。我创建了一个小提琴jsfiddle.net/guanxiaohua2k6/MUbe2。你可以试试看。
    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多