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

100
Visualizações
jQuery checkbox work as radio button but reverse back like checkbox

I have created input-type checkbox fields like below:

<input type="checkbox" class="" value="1" name="foo" />
<input type="checkbox" class="" value="1" name="foo" />
<input type="checkbox" class="" value="1" name="foo" />

Now i want checkbox work as radio button like single select. so using jquery is working fine:

$("input:checkbox").click(function(){
    var group = "input:checkbox[name='"+$(this).prop("name")+"']";
    $(group).prop("checked",false);
    $(this).prop("checked",true);
});

But now if i want to reverse that checkbox work as checkbox using jquery then not working.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is possible, but but you really shouldn't do it. It will lead to a poor user experience, because a user will generally assume that a set of checkboxes means they can select more than one if they wish to. If they see radio buttons, they know they have to make a single choice. If they find a checkbox set where they can't select more than one, it might be frustrating and they may even think there is a bug in your application.

Also, why would you need to "re-enable" anything? Once the question is answered, you would render a new question instead. You wouldn't re-use the same markup for a new question...that doesn't make any sense.

Instead, when rendering each question, make your code (either client-side or server-side, as per your rendering architecture) generate either checkboxes or radio buttons, as appropriate, depending on whether your database indicates that the question requires multiple or single answers. This should be both simpler than solutions which manipulate the checkbox behaviour, and will lead to a better UI.

about 4 years ago · Juan Pablo Isaza Relatório

0

You should use radio buttons, but if you must use checkbox:

$("input:checkbox").change(function(){
  $("input:checkbox").not(this).prop("checked",false);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You can unbind the click handler

    $("input:checkbox").click(function(){
        var group = "input:checkbox[name='"+$(this).prop("name")+"']";
        $(group).prop("checked",false);
        $(this).prop("checked",true);
    });
    function disableRadio ( ) {
        $("input:checkbox").unbind();
    }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <input type="checkbox" class="" value="1" name="foo" />
    <input type="checkbox" class="" value="1" name="foo" />
    <input type="checkbox" class="" value="1" name="foo" />   
    <button type="button" onclick="disableRadio()">Disable Radio</button> 
</body>
</html>

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