jQuery check/uncheck radio button onclick

前端 未结 24 2679
滥情空心
滥情空心 2020-12-01 05:23

I have this code to check/uncheck a radio button onclick.

I know it is not good for the UI, but I need this.

$(\'#radioinstant\').click(function() {          


        
24条回答
  •  眼角桃花
    2020-12-01 05:43

    Best and shortest solution. It will work for any group of radios (with the same name).

    $(document).ready(function(){
        $("input:radio:checked").data("chk",true);
        $("input:radio").click(function(){
            $("input[name='"+$(this).attr("name")+"']:radio").not(this).removeData("chk");
            $(this).data("chk",!$(this).data("chk"));
            $(this).prop("checked",$(this).data("chk"));
            $(this).button('refresh'); // in case you change the radio elements dynamically
        });
    });
    

    More information, here.

    Enjoy.

提交回复
热议问题