dimanche 22 novembre 2020

Checkboxes working like radio button, but how to uncheck?

<div id="checkboxes">
    <label><input type="checkbox" name="box1" value="orange" class="btn"> Oranges</label>
    <label><input type="checkbox" name="box2" value="apple" class="btn"> Apples</label>
</div>
<script type="text/javascript">
jQuery(function()
{
    jQuery(document).on("change", ".btn", function()
    {
        jQuery(".btn").prop("checked", false);

        if (! this.checked)
            jQuery(this).prop("checked", true);
    });
});
</script>

This works great to make checkboxes behave like radio buttons, but "unchecking" does not work. How to allow users to be able to uncheck as well?




Aucun commentaire:

Enregistrer un commentaire