i have a problem regarding to this link :Limiting default checked checkboxes.
I extend the code for validation of the script, Here's what i want to do.
Here's my sample code:
var mvp = 5;
$(document).ready(function() {
$("input:checkbox").each(function( index ) {
this.checked = ( index < mvp );
});
$("input:checkbox").click(function() {
var bol = $("input:checkbox:checked").length != mvp;
if(bol){
alert("You have to choose "+ mvp +" mvp's only");
this.checked = false;
$("#button-cart").attr("disabled", true);
}
else{
$("input:checkbox").not(":checked").attr("disabled",bol);
$("#button-cart").attr("disabled", false);
}
});
});
Here's the fiddle: fiddle
Based on the example i set the value of mvp in 5. the when the page loads there are default 5 checkbox checked. then if you uncheck 1 then the alert will trigger that it says you should have 5 mvp's, then the button will disable. once you check again a checkbox, the button will appear. but there are 2 left checkboxes that the user can select. but if the user exceed to 5 selections then the alert will trigger again and return it to 5 checked checkboxes. problem here is the button will remain disabled.
Can you help me to do this kind of logic :D thanks in advance..
Press the disable button and then the enable button. The checkbox doesn't get enabled. As the answers tell it is because the disabled attribute is a boolean attribute.
The disabled property sets or returns whether a checkbox should be disabled, or not. A disabled element is unusable and un-clickable. Disabled elements are usually rendered in gray by default in browsers. This property reflects the HTML disabled attribute.
Using attr() function attr('disabled'); This function also takes a parameter to specify the property or the task that the selected element or checkbox is applied to. Whereas this function specifies the property for the checkbox object and it is specified as “disabled” for disabling the checkbox object.
Usually you have to click the link to enable the checkbox to be checked. So below we will have a form that only lets you submit the information if you agree to the terms/conditions, by clicking the link then checking the checkbox.
Try this,
var mvp = 5;
$(document).ready(function () {
$("input:checkbox").each(function (index) {
this.checked = (index < mvp);
}).click(function () {
en_dis= $("input:checkbox:checked").length < mvp
$('#button-id').prop('disabled', en_dis);
// to disable other checkboxes
$('input:checkbox:checked').length >= mvp &&
$("input:checkbox:not(:checked)").prop('disabled',true);
}).change(function () {
if ($(this).siblings(':checked').length >= mvp) {
this.checked = false;
}
});
});
Working Demo and Updated Demo
You shouldn't set the checked property of the checkbox and also disable the button.
Here you go:
$(function () {
var $button = $('#button-id'),
mvp = 5;
$("input:checkbox").each(function (index) {
this.checked = (index < mvp);
}).on("click", function () {
if ($("input:checkbox:checked").length != mvp) {
alert("You have to choose " + mvp + " mvp's only");
$button.prop("disabled", true);
} else {
$("input:checkbox").not(":checked").prop("disabled", false);
$button.prop("disabled", false);
}
});
});
Working demo: http://jsfiddle.net/R79cM/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With