jquery - Select only one checkbox in group -
i need in html select 1 checkbox in group. find example. can't realize it. doesn't works. here example: example
here code html:
<div> <li> <div class="radio"> <label> <input type="checkbox" name="mobil[1][]" id="optionsradios1" value="1" checked class="radi"> Сотовый телефон имеется </label> </div> </li> <li> <div class="radio"> <label> <input type="checkbox" name="mobil[1][]" id="optionsradios2" value="0" class="radi"> Сотовый телефон не имеется </label> </div> </li> <div>
and code jquery:
$("input:checkbox").on('click', function() { // in handler, 'this' refers box clicked on var $box = $(this); if ($box.is(":checked")) { // name of box retrieved using .attr() method // assumed , expected immutable var group = "input:checkbox[name='" + $box.attr("name") + "']"; // checked state of group/box on other hand change // , current value retrieved using .prop() method $(group).prop("checked", false); $box.prop("checked", true); } else { $box.prop("checked", false); } });
any plz.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> $(function() { $("input:checkbox").on('click', function() { // in handler, 'this' refers box clicked on var $box = $(this); if ($box.is(":checked")) { // name of box retrieved using .attr() method // assumed , expected immutable var group = "input:checkbox[name='" + $box.attr("name") + "']"; // checked state of group/box on other hand change // , current value retrieved using .prop() method $(group).prop("checked", false); $box.prop("checked", true); } else { $box.prop("checked", false); } }); }); </script> <form> <div> <li> <div class="radio"> <label> <input type="checkbox" name="mobil[1][]" id="optionsradios1" value="1" checked class="radi">Сотовый телефон имеется </label> </div> </li> <li> <div class="radio"> <label> <input type="checkbox" name="mobil[1][]" id="optionsradios2" value="0" class="radi">Сотовый телефон не имеется </label> </div> </li> <div> </form>
your provided sample works here.
Comments
Post a Comment