Hướng dẫn chỉ cho phép chọn 1 trong nhiều checkbox html

Admin

AdminAdmin is verified member.

Well-Known Member
Staff member
Administrator
Chào các bạn, hôm nay tuoitreit.vn xin hướng dẫn bạn cách chỉ cho phép chọn 1 trong nhiều check box
Bạn post như sau nhé
JavaScript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  $(function() {
    $("input:checkbox").on('click', function() {
      // in the handler, 'this' refers to the box clicked on
      var $box = $(this);
      if ($box.is(":checked")) {
        // the name of the box is retrieved using the .attr() method
        // as it is assumed and expected to be immutable
        var group = "input:checkbox[name='" + $box.attr("name") + "']";
        // the checked state of the group/box on the other hand will change
        // and the current value is retrieved using .prop() method
        $(group).prop("checked", false);
        $box.prop("checked", true);
      } else {
        $box.prop("checked", false);
      }
    });
  });
</script>
HTML:
<input type="checkbox" name="kiemtra" value="tuoitreit.vn" checked/>
<input type="checkbox" name="kiemtra2" value="tuoitreit.vn"/>
Chúc bạn thành công!
Mọi sao chép vui lòng ghi rõ nguồn tuoitreit.vn
 

Facebook Comments

Similar threads

Admin
Replies
0
Views
3K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
3K
AdminAdmin is verified member.
Admin
Back
Top