How to mark a specific Checkboxlist item with jquery

Asked

Viewed 90 times

0

I have seen several examples of how to mark and unmark all, but I have not seen how to mark a specific, see:

<asp:CheckBoxList ID="ChkAcoesListarGrupo" runat="server" RepeatDirection="Horizontal" Width="100%" >
                                <asp:ListItem Value="1">Novo</asp:ListItem>
                                <asp:ListItem Value="2">Atualizar</asp:ListItem>
                                <asp:ListItem Value="3">Excluir</asp:ListItem>
                                <asp:ListItem Value="4">Visualizar</asp:ListItem>
                                <asp:ListItem Value="5">Pesquisar</asp:ListItem>
                            </asp:CheckBoxList>

How do I leave the item Visualizar marked with jquery ?

It would be something like that:

$('#<%= ddlPerfil.ClientID %>').change(function (e) {
                e.preventDefault();
//Aqui eu marcaria o item **Visualizar**
});
  • Because you just don’t put in the checkbox that you want to leave marked attribute checked ?

  • @hugocsl like that $("[id*=chkAcoesListarGrupo] input:checkbox").prop('checked', false); i mark and/or deselect all of the list but how do I assign checked solely for the item Visualizar ?

1 answer

1

With jQuery you can check one checkbox thus .prop('checked', true), using the value attribute $('[value="teste"]')

See example working.

$("#btn").click(function() {
    $('[value="teste"]').prop('checked', true);
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<div id="btn">clicar</div>

<input type="checkbox" name="" id="teste" value="teste">

<p>vc tb pode deixar marcado direto no load, <i>"$(document).ready(function(){ "</i></p>


Via HTML

I don’t know if this is exactly what you need but because you don’t use the attribute checked of html? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#checked

Take this example:

<div class="btns">
  <input type="checkbox" name="" id="">item 1<br>
  <input checked type="checkbox" name="" id="">checado<br>
  <input type="checkbox" name="" id="">item 3<br>
  <input type="checkbox" name="" id="">item 4<br>
</div>

  • I may have the following situation: <input type="checkbox" name="" id="teste" value="teste">; <input type="checkbox" name="" id="teste_1" value="teste">; <input type="checkbox" name="" id="teste_2" value="teste">;

  • in jaquery event as I will distinguish which checkbox ?

  • If everyone has the same Thanks and different ID then you get by the ID, simple like this $('[id="teste_1"]') that would be simplified $('#teste_1').

  • @adrianojc Vc tb can take the "grossness the second item on the list this way, if all have the same value, ai the second item that has the same value you apply the checked... $('[value="teste"]:nth-of-type(2)') where you see the number 2 is the position of the item in the list, if you have 4 items and you want to select the Fourth item you would do $('[value="teste"]:nth-of-type(4)')

Browser other questions tagged

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