
Admin
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é
Chúc bạn thành công!
Mọi sao chép vui lòng ghi rõ nguồn tuoitreit.vn
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"/>
Mọi sao chép vui lòng ghi rõ nguồn tuoitreit.vn