开发者

radio button matrix group javascript jquery

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) (o) (o)

3 (o) (o) (o)

<table>   
<tr> 
<td>1</td> 
<td><input type="radio" id="ljudkalla_1" name="ljudkalla_1" value="Radio A" onclick="checkMatrixRow(this)"></td> 
<td><input type="radio" id="ljudkalla_2" name="ljudkalla_2" value="Radio B" onclick="checkMatrixRow(this)"></td> 
<td><input type="radio" id="ljudkalla_3" name="ljudkalla_3" value="Ipod A" onclick="checkMatrixRow(this)"></td> 
</tr>
<tr> 
<td>2</td> 
<td><input type="radio" id="ljudkalla_1" name="ljudkalla_1" value="Radio A" onclick="checkMatrixRow(this)"></td> 
<td><input type="radio" id="ljudkalla_2" name="ljudkalla_2" value="Radio B" onclick="checkMatrixRow(this)"></td> 
<td><input type="radio" id="ljudkalla_3" name="ljudkalla_3" value="Ipod A" onclick="checkMatrixRow(this)"></td> 
</tr>
<tr> 
<td>3</td> 
<td><input type="radio" id="ljudkalla_1" name="ljudkalla_1" value="Radio A" onclick="checkMatrixRow(this)"></td> 
<td><input type="radio" id="ljudkalla_2" name="ljudkalla_2" value="Radio B" onclick="checkMatrixRow(this)"></td> 
<td><input type="radio" id="ljudkalla_3" name="ljudkalla_3" value="Ipod A" onclick="checkMatrixRow(this)"></td> 
</tr>
</table>

<script>
// radio buttons
var columns = new    Array('ljudkalla_1','ljudkalla_2','ljudkalla_3');

function getSelectedIndex(array) {
for (var i=0; i<array.length; i++) {
    if (array[i].checked) return i;
}
}

function checkMatrixRow(input) {

var n = getSelectedIndex(input.form[input.name]); // index of selected button in a radio group (= row number)   


for (var i=0; i<columns.length; i++) {
    if (columns[i] != input.name) {
        input.form[columns[i]][n].checked = false;
    }
}
}
</script>

I should only be able to choose one button per row and column, so if I have chosen A1 and then click on B1, the first one should uncheck. The big issue is really the C column. In this column I should be able to choose all three, C1 C2 and C3, but same thing here if I have chosen C2 and then check A2 the first one should uncheck. I have been scanning the internet for days and I cannot find anything, so if anyone knows of a tutorial or just some information on how to solve this I would be deeply grateful.

Or perhaps it is not possible to do it this way?

Thanks

Linda


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


<table>   
<tr> 
<td>1</td> 
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_1" value="Radio A"></td> 
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_1" value="Radio B"></td> 
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_1" value="Ipod A"></td> 
</tr>
<tr> 
<td>2</td> 
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_2" value="Radio A"></td> 
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_2" value="Radio B"></td> 
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_2" value="Ipod A"></td> 
</tr>
<tr> 
<td>3</td> 
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_3" value="Radio A"></td> 
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_3" value="Radio B"></td> 
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_3" value="Ipod A"></td> 
</tr>
</table>

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.


You may use my plugin https://github.com/zevero/jquery-checkbox-table

$('#container').checkbox_table({
   cols: ['A','B','C'],
   rows: [1,2,3]
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜