Jquery Get Checkbox Value Nearer by Line

2

I need help with the following:

Iwanttogetthecheckboxthatisdisabledandplayonconsole0forwhatisdisabled,and1forwhatisenabled.

Mysourceislikethis:(asp.netmvc)

@modelIEnumerable<Apontamento.Models.Modulos>@{ViewBag.Title="Acesso";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

<h2>Acesso</h2>
<br />
<table class="table table-condensed table-striped">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.cod_modulo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.nome_modulo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.usuario)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.permissao)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="linha" data-codigo_modulo="@item.cod_modulo" data-usuario="@item.usuario">
            <td>
                @Html.DisplayFor(modelItem => item.cod_modulo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.nome_modulo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.usuario)
            </td>
            <td>
                @Html.EditorFor(modelItem => item.permissao)
            </td>
        </tr>
    }
</table>
<script type="text/javascript">
    $(document).ready(function () {
        $(".linha").each(function () {
            var codigo_modulo = $(this).data("codigo_modulo");
            var usuario = $(this).data("usuario");
            var valor_atual = 0;
            if ($(this).next("input").has("checked")) {
                valor_atual = 1;
            } else {
                valor_atual = 0;
            }
            console.log(codigo_modulo + " " + usuario + " " + valor_atual);
        });
    });

</script>

I'm definitely in this jquery here:

<script type="text/javascript">
    $(document).ready(function () {
        $(".linha").each(function () {
            var codigo_modulo = $(this).data("codigo_modulo");
            var usuario = $(this).data("usuario");
            var valor_atual = 0;
            if ($(this).next("input").has("checked")) {
                valor_atual = 1;
            } else {
                valor_atual = 0;
            }
            console.log(codigo_modulo + " " + usuario + " " + valor_atual);
        });
    });

</script>

I play console, but he plays like this (1 for everything)

Imadea$(".linha").each(function () { to go through each line, the code of the module and the name is getting right, but this condition is not working:

var valor_atual = 0;
if ($(this).next("input").has("checked")) {
    valor_atual = 1;
} else {
    valor_atual = 0;
}

Can anyone help me with how to test whether the checkbox in the row is checked or not?

If it helps, the page source looks like this: (in the table) (notice that the second input is not with checked="checked"

<table class="table table-condensed table-striped">
    <tr>
        <th>
            C&#243;digo Modulo
        </th>
        <th>
            Nome do M&#243;dulo
        </th>
        <th>
            usuario
        </th>
        <th>
            permissao
        </th>
    </tr>

        <tr class="linha" data-codigo_modulo="1" data-usuario="dayane.fiedler">
            <td>
                1
            </td>
            <td>
                Produtos
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="2" data-usuario="dayane.fiedler">
            <td>
                2
            </td>
            <td>
                Usu&#225;rios
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="3" data-usuario="dayane.fiedler">
            <td>
                3
            </td>
            <td>
                Ordem Produ&#231;&#227;o
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="5" data-usuario="dayane.fiedler">
            <td>
                5
            </td>
            <td>
                Adminstrador
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="6" data-usuario="dayane.fiedler">
            <td>
                6
            </td>
            <td>
                Opera&#231;&#245;es
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
</table>
    
asked by anonymous 13.09.2018 / 18:05

1 answer

1

You're doing it wrong while fetching input . The .next() will select the next element of the selector and not search the input.

Just swap if for this:

if ($(this).find("input").is(":checked")) {

The .find() looks for the element within the selector and the .is(":checked") checks to see if it is checked.

See:

$(document).ready(function () {
        $(".linha").each(function () {
            var codigo_modulo = $(this).data("codigo_modulo");
            var usuario = $(this).data("usuario");
            var valor_atual = 0;
            if ($(this).find("input").is(":checked")) {
                valor_atual = 1;
            } else {
                valor_atual = 0;
            }
            console.log(codigo_modulo + " " + usuario + " " + valor_atual);
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tableclass="table table-condensed table-striped">
    <tr>
        <th>
            C&#243;digo Modulo
        </th>
        <th>
            Nome do M&#243;dulo
        </th>
        <th>
            usuario
        </th>
        <th>
            permissao
        </th>
    </tr>

        <tr class="linha" data-codigo_modulo="1" data-usuario="dayane.fiedler">
            <td>
                1
            </td>
            <td>
                Produtos
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="2" data-usuario="dayane.fiedler">
            <td>
                2
            </td>
            <td>
                Usu&#225;rios
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="3" data-usuario="dayane.fiedler">
            <td>
                3
            </td>
            <td>
                Ordem Produ&#231;&#227;o
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="5" data-usuario="dayane.fiedler">
            <td>
                5
            </td>
            <td>
                Adminstrador
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
        <tr class="linha" data-codigo_modulo="6" data-usuario="dayane.fiedler">
            <td>
                6
            </td>
            <td>
                Opera&#231;&#245;es
            </td>
            <td>
                dayane.fiedler
            </td>
            <td>
                <input checked="checked" class="check-box" data-val="true" data-val-required="O campo permissao é obrigatório." id="item_permissao" name="item.permissao" type="checkbox" value="true" /><input name="item.permissao" type="hidden" value="false" />
            </td>
        </tr>
</table>
  

Note: Do not repeat id item_permissao . A id must be unique on the page.

    
13.09.2018 / 19:52