【问题标题】:Updating child element classes when table row is clicked单击表格行时更新子元素类
【发布时间】:2016-02-11 15:13:31
【问题描述】:

我有一张产品选项表,每个tr 都是一个选项。默认选择一个,要选择另一个,必须在提交表单之前选中一个单选按钮。这很容易,但整个tr 必须是可点击的,并相应地选中单选按钮。

认为我已经设法使用下面的脚本让它工作:

$('.product-options tr').click(function() {
    $(this).find('td input[type=radio]').prop('checked', true);
});

除此之外我还需要在tr 中添加一个.selected 的类。我尝试使用以下代码,但我还需要检查另一个 tr 是否已经有一个 .selected 类并将其删除。

$('.product-options tr').click(function() {
    $(this).find('td input[type=radio]').prop('checked', true);
    $(this).toggleClass('selected', true);
});

显然,如果您单击相同的单选按钮,这只会切换类。这就是我有限的javascript知识失败的地方。有人可以帮忙吗?

最后,我还使用了一个名为 uniform.js 的插件,它允许选择/收音机/复选框完全可定制。由于它包装了一个 div,然后围绕无线电输入的一个跨度将一个 .checked 类添加到跨度以切换选中/未选中样式。仅当您直接单击输入时才会更改。因此,当单击 tr 的任何部分时,我还需要考虑与 tr.selected 类类似的脚本,自定义单选按钮也有一个更新的类。

对于 javascript 生成的收音机的参考标记是:

<div class="radio">
    <span>
        <input type="radio" name="" />
    </span>
</div>

编辑:这是一个包含 uniform.js 的 CodePen,它应该可以更好地展示问题:

http://codepen.io/moy/pen/yeGRmO

谢谢,真的希望有人可以帮助解决这个问题。它似乎很简单......但是因为它是“分层的”,我有点迷路了! :)

【问题讨论】:

    标签: javascript jquery html toggleclass uniform


    【解决方案1】:

    如果我理解正确,您可以简单地访问 tr 的同级并将选中的收音机设置为 false。

    $('.product-options tr').click(function() {
        $(this).find('td input[type=radio]').prop('checked', true);
        $(this).toggleClass('selected', true);
        $(this).siblings().find('td input[type=radio]').prop('checked', false);
        $(this).siblings().removeClass('selected');
    });
    

    这应该找到所有收音机并将它们全部设置为 false,除了您单击的行。

    更新

    要使用制服,您可以在更改属性时直接调用$.uniform.update()

    $(function() {
        $('input[type="radio"]').uniform();
    
        $('.product-options tr').click(function() {
            var tr = $(this),
                radio = tr.find('td input[type=radio]'),
                siblings = tr.siblings(),
                otherRadios = siblings.find('td input[type=radio]');
    
            tr.addClass('selected');
            siblings.toggleClass('selected', false);
    
            $.uniform.update(radio.prop('checked', true));
            $.uniform.update(otherRadios.prop('checked', false));
        });
    });
    

    DEMO

    【讨论】:

    • 谢谢,我试过了,但不幸的是,如果单击另一个,它不会从先前选择的tr 中删除selected 的类。收音机也被包裹在 div 中以进行造型。所以“已检查”状态有效,但它还需要向其父级添加一个类,以便“已检查”样式可见。
    • 查看演示,因为我只是要更新那部分。
    • 统一的收音机通常会自行更新,不是吗?如果不是额外的radio.closest('.radio') 可以在他的父母上提供一个类。与$.each() 类似,其他无线电清除课程。
    • 是的,如果您直接单击元素,但在单击表格行时不会。必须关闭点击而不是收音机的状态?
    • 更新了代码,使用了统一的示例。所以这应该与您的设置类似。
    【解决方案2】:

    试试这个:

    $('.product-options tr').click(function () {
                $(this).find('td input[type=radio]').prop('checked', true);
                $(this).closest('table').find('tr.selected').find('span.checked').removeClass('checked');
                $(this).closest('table').find('tr.selected').removeClass('selected');
                $(this).addClass('selected'); $(this).find('td:last').find('span').addClass('checked');
            });
    

    Check This Link

    【讨论】:

    • 谢谢,我无法让它工作,但如果有帮助,我已经添加了一个 CodePen:codepen.io/moy/pen/yeGRmO 可能会更清楚地显示问题!干杯!
    • 非常感谢,太好了! :)
    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多