You can set a custom data-max
attribute on each input field, which is filled through the database:
<input
name="<?php echo $field["name"]; ?>"
type="checkbox" value="<?php echo $field["value"]; ?>"
data-max="<?php echo $field["max_checked"]; ?>"
/>
Then you can use the following checks in your javascript event handler:
$("input[type=checkbox]").change(function () {
var InputName = $(this).attr("name");
var MaxAllowed = $(this).attr("data-max");
// Get amount of checked boxes with the same name
if( $("input[name=" + InputName + "]:checked").length >= MaxAllowed ){
// Disable the remaining checkboxes of the same name
$("input[name=" + InputName + "]").not(":checked").attr("disabled", "disabled");
} else {
// Enable the inputs again when he unchecks one
$("input[name=" + InputName + "]").removeAttr("disabled");
});