do a checkbox validation per row of a table

0

I have a page with a table where each row has 3 checkboxes and each column has 11 rows with 3 checkboxes, I needed to do a validation where at least one checkbox of 3 of each row is checked, and if on a row there are 3 checkbox unchecked after an error message I tried this logic and it is not working

$("#submit").click(function(){
                if($('#ok').not(":checked") && $('#nok').not(":checked") && $('#na').not(":checked")){
                    alert('erro');
                }else if($('#ok1').not(":checked") && $('#nok1').not(":checked") && $('#na1').not(":checked")){
                    alert('erro');
                }else if($('#ok2').not(":checked") && $('#nok2').not(":checked") && $('#na2').not(":checked")){
                    alert('erro');
                }else if($('#ok3').not(":checked") && $('#nok3').not(":checked") && $('#na3').not(":checked")){
                    alert('erro');
                }else if($('#ok4').not(":checked") && $('#nok4').not(":checked") && $('#na4').not(":checked")){
                    alert('erro');
                }else if($('#ok5').not(":checked") && $('#nok5').not(":checked") && $('#na5').not(":checked")){
                    alert('erro');
                }else if($('#ok6').not(":checked") && $('#nok6').not(":checked") && $('#na6').not(":checked")){
                    alert('erro');
                }else if($('#ok7').not(":checked") && $('#nok7').not(":checked") && $('#na7').not(":checked")){
                    alert('erro');
                }else if($('#ok8').not(":checked") && $('#nok8').not(":checked") && $('#na8').not(":checked")){
                    alert('erro');
                }else if($('#ok9').not(":checked") && $('#nok9').not(":checked") && $('#na9').not(":checked")){
                    alert('erro');
                }else if($('#ok10').not(":checked") && $('#nok10').not(":checked") && $('#na10').not(":checked")){
                    alert('erro');
                }else if($('#sim').not(":checked") && $('#nao').not(":checked")){
                    alert('erro');
                }else {
                    alert("checado");
                }
        }); 

Someone can tell me where I'm going wrong, the idea is that of the 11 lines 1 line has the s 3 checkbox and of these 3 checkbox 1 must be marked for each line of 11

table line1 [x] [] [] line2 [] [x] [] line3 [] [x] []

If you have all of them with a dialup otherwise of the error as in the example below

table line1 [x] [] [] line2 [] [] []

asked by anonymous 22.07.2016 / 03:32

1 answer

2

Better to do dynamically, if you have to check more rows you will have to be changing the code every time you add a new one.

$(document).ready(function() {

    $('#check').click(function(event) {
        event.preventDefault();
        var valido = true;

        $("#tabela tr").each(function(index, el) {
            var $linha = $(el);
            var checked = $linha.find('.opcao:checked').length;
            if (checked == 0 ) {
                valido = false;
                return false;
            }
        });
        console.log('valido ' , valido);
        alert(valido ? 'checado' : 'erro');
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tableid="tabela">
        <tr>
            <td><input type="checkbox" class="opcao"></td>
            <td><input type="checkbox" class="opcao"></td>
            <td><input type="checkbox" class="opcao"></td>
        </tr>
        <tr>
            <td><input type="checkbox" class="opcao"></td>
            <td><input type="checkbox" class="opcao"></td>
            <td><input type="checkbox" class="opcao"></td>
        </tr>
        <tr>
            <td><input type="checkbox" class="opcao"></td>
            <td><input type="checkbox" class="opcao"></td>
            <td><input type="checkbox" class="opcao"></td>
        </tr>
    </table>
    <button id="check"> Validar</button>
    
22.07.2016 / 04:01