Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
How to make checkbox checked according to a value in Jquery

I need to make checked status of my checkbox according to a data value. Up to now I am using below method.

$(document).ready(function(){  
      $('#add').click(function(){  
           $('#insert').val("Insert");  
           $('#update_form')[0].reset();  
      });  
      $(document).on('click', '.update_data', function(){ 
           var row_id = $(this).attr("id");  
           
           $.ajax({  
                url:"./project/userdetail.php",  
                method:"POST",  
                data:{row_id:row_id},  
                dataType:"json", 
                success:function(data){  
                      ...........
                     if(data.accesslocations=='ABC,DEF,GHI'){
                      document.getElementById("check1").checked = true;
                      document.getElementById("check2").checked = true;
                      document.getElementById("check3").checked = true;
                      
                     }

                     if(data.accesslocations=='ABC,GHI'){
                      document.getElementById("check1").checked = true;
                      document.getElementById("check3").checked = true;
                      
                     }
                     if(data.accesslocations=='ABC'){
                      document.getElementById("check1").checked = true;
                     }

                    
                     
                     
                     ...........

                 $('#employee_id_update').val(data.row_id);  
                 $('#insert').val("Update");
                 $('#new_data_Modal').modal('show'); 
                        
                      
                }, 
                error: function(req, status, error) {
               alert(req.responseText);      
                }   
           });  
      });  

My intention is to check the data.accesslocations and check whether it contains ABC or ABC,DEF likewise,

If ABC contains need to check1 status as checked.
If DEF contains need to check2 status as checked.
If GHI contains need to check3 status as checked.

I have tried below method as well. But then I could not open my dialogbox, new_data_Modal.

               if(strstr(data.accesslocations, "ABC")){
                  document.getElementById("check1").checked = true;
                 }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • Create an Array out of your string "GHI,ABC" → ["ABC", "GHI"] using String.prototype.split()
  • After you have you array, go for your elements values using Array.prototype.includes()

const handleFoobarCheckboxes = (str) => {
  const values = str.split(",");
  document.querySelectorAll('[name="foobar[]"]').forEach(elCkb => {
    elCkb.checked = values.includes(elCkb.value);
  });
};

handleFoobarCheckboxes("GHI,ABC");
<label><input name="foobar[]" type="checkbox" value="ABC"> ABC</label>
<label><input name="foobar[]" type="checkbox" value="DEF"> DEF</label>
<label><input name="foobar[]" type="checkbox" value="GHI"> GHI</label>

as you can see from the example above, the order of values will not matter and your script will still work.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda