perform a checkbox validation per row of a table

Asked

Viewed 600 times

0

I have a page with a table where each row has 3 checkbox and each column has 11 lines with the 3 checkbox, I needed to do a validation where at least one checkbox of the 3 of each row is marked, and if on a line you have the 3 checkbox unchecked behind an error message I tried that logic and it’s 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 this marked of each line of the 11

table line1 [x] [] [] Linha2 [] [x] [] line3 [] [x] []

if all of them have a registered mark otherwise error as in the example below

table line1 [x] [] [] Linha2 [] [] [] <= error without marking line3 [] [x] []

1 answer

2

Better to do it dynamically, if you have to check more lines you will have to change 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>
<table id="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.