当用户单击某个单选按钮时,如何使用 jQuery 将一个类仅分配给一组中的一个单选按钮?

发布于 2024-08-31 18:01:18 字数 2029 浏览 1 评论 0原文

我有以下标记。当用户点击

(包含单选按钮和标签)时,我想将 class_A 添加到

(包含单选按钮和标签)代码> 或<代码><标签>。

如果用户单击同一组中的某些其他单选按钮/标签,我想将 class_A 添加到该单选按钮的父段落中,并从包含该组中的单选按钮/标签的任何其他段落中删除 class_A 。实际上,在每个

  • 中,只有一个

    应添加 class_A。

  • 有没有 jQuery 插件可以做到这一点?或者有一个简单的技巧可以做到这一点吗?

    <ul>
    <li>
        <div class="myitem-wrapper" id="10"> 
            <div class="myitem clearfix">
                <span class="number">1</span>
                <div class="item-text">Some text here </div>
            </div>
            <p class="subitem-text">
                <input type="radio" name="10" value="15" id="99">
                <label for="99">First subitem </label>
            </p>
            <p class="subitem-text">
                <input type="radio" name="10" value="77" id="21">
                <label for="21">Second subitem</label>
            </p>
    
        </div>
    </li>
    
    <li>
        <div class="myitem-wrapper" id="11"> 
            <div class="myitem clearfix">
                <span class="number">2</span>
                <div class="item-text">Some other text here ... </div>
            </div>
            <p class="subitem-text">
                <input type="radio" name="11" value="32" id="201">
                <label for="201">First subitem ... </label>
            </p>
            <p class="subitem-text">
                <input type="radio" name="11" value="68" id="205">
                <label for="205">Second subitem ...</label>
            </p>
    
            <p class="subitem-text">
                <input type="radio" name="11" value="160" id="206">
                <label for="206">Third subitem ...</label>
            </p>
        </div>
    </li>
    </ul>   
    

    I have the following markup. I would like to add class_A to <p class="subitem-text"> (that holds the radio button and the label) when user clicks on the <input> or <label>.

    If user clicks some other radio-button/label in the same group, I would like to add class_A to this radio-button's parent paragraph and remove class_A from any other paragraph that hold radio-buttons/labels in that group. Effectively, in each <li>, only one <p class="subitem-text"> should have class_A added to it.

    Is there a jQuery plug-in that does this? Or is there a simple trick that can do this?

    <ul>
    <li>
        <div class="myitem-wrapper" id="10"> 
            <div class="myitem clearfix">
                <span class="number">1</span>
                <div class="item-text">Some text here </div>
            </div>
            <p class="subitem-text">
                <input type="radio" name="10" value="15" id="99">
                <label for="99">First subitem </label>
            </p>
            <p class="subitem-text">
                <input type="radio" name="10" value="77" id="21">
                <label for="21">Second subitem</label>
            </p>
    
        </div>
    </li>
    
    <li>
        <div class="myitem-wrapper" id="11"> 
            <div class="myitem clearfix">
                <span class="number">2</span>
                <div class="item-text">Some other text here ... </div>
            </div>
            <p class="subitem-text">
                <input type="radio" name="11" value="32" id="201">
                <label for="201">First subitem ... </label>
            </p>
            <p class="subitem-text">
                <input type="radio" name="11" value="68" id="205">
                <label for="205">Second subitem ...</label>
            </p>
    
            <p class="subitem-text">
                <input type="radio" name="11" value="160" id="206">
                <label for="206">Third subitem ...</label>
            </p>
        </div>
    </li>
    </ul>   
    

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(4

    后知后觉 2024-09-07 18:01:18

    演示: http://jsbin.com/ebaye3

    因为您将所有内容都放入其中P你可以用它!

    $(function($) {
        $('.subitem-text').click(function() {
            $(this).parent().find('.subitem-text').removeClass('class_A');
            if ( $(this).children(':radio').is(':checked') ) // for sake! ;-)
            $(this).addClass('class_A');
        });
       //you can also write it like this:
    
      $('.subitem-text :radio').click(function() {
        $(this).parent().parent().children().removeClass('class_A');
        if ( $(this).is(':checked') )
        $(this).parent().addClass('class_A');
       });
    
    });
    

    DEMO: http://jsbin.com/ebaye3

    since you are putting all inside P you can use it!

    $(function($) {
        $('.subitem-text').click(function() {
            $(this).parent().find('.subitem-text').removeClass('class_A');
            if ( $(this).children(':radio').is(':checked') ) // for sake! ;-)
            $(this).addClass('class_A');
        });
       //you can also write it like this:
    
      $('.subitem-text :radio').click(function() {
        $(this).parent().parent().children().removeClass('class_A');
        if ( $(this).is(':checked') )
        $(this).parent().addClass('class_A');
       });
    
    });
    
    滥情稳全场 2024-09-07 18:01:18

    你可以这样做:

      // find all the radio inputs inside a subitem-text
      $('.subitem-text input[type=radio]').bind('change', function() {
        // find our parent LI
        var $li = $(this).closest('li'); 
        // remove any "class_A" 
        $li.find('.class_A').removeClass('class_A');
        // find the subitem with the checked input and add "class_A"
        $li.find('.subitem-text:has(input[checked])').addClass('class_A');
      });
    

    jsbin Preview/demo

    You can do something like this:

      // find all the radio inputs inside a subitem-text
      $('.subitem-text input[type=radio]').bind('change', function() {
        // find our parent LI
        var $li = $(this).closest('li'); 
        // remove any "class_A" 
        $li.find('.class_A').removeClass('class_A');
        // find the subitem with the checked input and add "class_A"
        $li.find('.subitem-text:has(input[checked])').addClass('class_A');
      });
    

    jsbin preview/demo

    梦行七里 2024-09-07 18:01:18

    像这样:

    $(':radio').click(function() {
        $(this).closest('ul').find('.subitem-text').removeClass('active');
    
        $(this).closest('.subitem-text').addClass('active');
    });
    

    Like this:

    $(':radio').click(function() {
        $(this).closest('ul').find('.subitem-text').removeClass('active');
    
        $(this).closest('.subitem-text').addClass('active');
    });
    
    云裳 2024-09-07 18:01:18

    没有固定的方法可以做到这一点,因为它依赖于文档中的 html。最简单的方法是绑定到每个无线电输入元素,但您也可以使用事件委托并绑定到 div.myitem-wrapper、li、父 ul 或事件正文标记。

    只需将点击处理程序绑定到我们感兴趣的每个输入元素:

    $("div.myitem-wrapper input[type=radio]").bind('change', function (event) {
        $(event.target).is(':checked').closest('p')
            .addClass('class_A')
            .siblings('p.subitem-text').removeClass('class_A');
    });
    

    同样的事情,但使用事件委托将处理程序的数量减少到一个。如果您发现要绑定到大量元素,这确实可以加快速度。

    $("#id_of_the_parent_UL").bind('change', function (event) {
        var $target = $(event.target);
        if ($target.is('input[type=radio]:checked')) {
            $target.closest('p')
                .addClass('class_A')
                .siblings('p.subitem-text').removeClass('class_A');
        }
    });
    

    请注意,使用 $(this) 而不是 $(event.target) 是完全有效的,但如果您开始转向事件委托,它会给出不同的结果。另一个好处是,如果您使用 事件.target

    There is no set way to do this since it is dependent on the html in your document. The simplest way to do this is to bind to each of your radio input elements but you can also use event delegation and bind to the div.myitem-wrapper, li, parent ul or event body tag.

    Just binding click handlers to each of the input elements we are interested in:

    $("div.myitem-wrapper input[type=radio]").bind('change', function (event) {
        $(event.target).is(':checked').closest('p')
            .addClass('class_A')
            .siblings('p.subitem-text').removeClass('class_A');
    });
    

    Same thing but using event delegation to reduce the number of handlers to one. This can really speed things up if you find that you are binding to a large number of elements.

    $("#id_of_the_parent_UL").bind('change', function (event) {
        var $target = $(event.target);
        if ($target.is('input[type=radio]:checked')) {
            $target.closest('p')
                .addClass('class_A')
                .siblings('p.subitem-text').removeClass('class_A');
        }
    });
    

    Note that it is perfectly valid to say $(this) instead of $(event.target) but it will give different results if and when you start moving to event delegation. An added advantage is that it will be easier for you or the next guy to understand the code in in two months if you go with event.target.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文