radio button matrix group javascript jquery

后端 未结 2 501
北恋
北恋 2021-01-28 23:54

I am completely lost as to how I can solve this. I need to create a matrix of radio buttons, column 1 to 3 and rows A to C.
A B C
1 (o) (o) (o)
2 (o)

2条回答
  •  有刺的猬
    2021-01-29 00:32

    I some how stumbled upon the answer while playing with it O_o

    Demo

    Basically you use the class name in conjunction with the name attribute to get a multi-axis radio button. Then the radio buttons are reset based on the same name and you reset the others based on class name. Elegant yet simple.

    Markup


    1
    2
    3

    jQuery


    $("input").click(function(){
        $("input."+this.className).not($(this)).each(function(){
            this.checked = false;
        });
    });
    

    Edit


    Demo 2

    I added another cool little feature to this with the following code

    $("input").click(function(){
        $("input."+this.className).not($(this)).each(function(){
            this.checked = false;
        });
        $("input:not([name='"+this.name+"'])").each(function(){
            if ($("input[name='"+this.name+"']:checked").length < 1)
                if($("input."+this.className+":checked").length < 1)
                    this.checked = true;
        });
    });
    

    This enables it to automatically change a radio button selection if another selection deselects it... maybe you should just see the demo. :P It's a little hard to explain I guess.

提交回复
热议问题