【问题标题】:On click button Check and Uncheck multiple radio buttons via Jquery单击按钮通过 Jquery 检查和取消选中多个单选按钮
【发布时间】:2023-02-19 17:55:30
【问题描述】:

根据图像,我想选中和取消选中单选按钮,因为它们都存在和不存在。我写了一些代码,但它只工作一次。

$(document).on('click', '#btn_all_absent', function(e){

                $("input:radio[class^=present]").each(function(i) {
                    $(this).attr('checked',false);
                });

                $("input:radio[class^=absent]").each(function(i) {
                    $(this).attr('checked',true);
                });
            }); 


            $(document).on('click', '#btn_all_present', function(e){
                
                $("input:radio[class^=absent]").each(function(i) {
                    $(this).attr('checked',false);
                });

                $("input:radio[class^=present]").each(function(i) {
                    $(this).attr('checked',true);
                });
            }); 

请建议我哪里错了。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    用 .prop() 试试这个版本:

    $(document).on('click', '#btn_all_absent', function(e){
        $("input:radio[class^=present]").prop('checked', false);
        $("input:radio[class^=absent]").prop('checked', true);
    });
    
    $(document).on('click', '#btn_all_present', function(e){
        $("input:radio[class^=absent]").prop('checked', false);
        $("input:radio[class^=present]").prop('checked', true);
    });
    

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 2012-06-08
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多