I have five checkboxes. Using jQuery, how do I check if at least one of them is checked?
<
var atLeastOneIsChecked = $('input[name="service[]"]:checked').length > 0;
This should do the trick:
function isOneChecked() {
return ($('[name="service[]"]:checked').length > 0);
}
var checkboxes = document.getElementsByName("service[]");
if ([].some.call(checkboxes, function () { return this.checked; })) {
// code
}
What you want is simple, get all the elements with the name, then run some code if some of those elements are checked.
No need for jQuery.
You may need an ES5 shim for legacy browsers though
You can do the following way. Initially set a variable, lets say checked
as false
. Then set it to true
if the following condition met. Use an if
statement to check the variable. Take note: Here submit
is the id
of the button, main
is the id of the form.
$("#submit").click(function() {
var checked = false;
if (jQuery('#main input[type=checkbox]:checked').length) {
checked = true;
}
if (!checked) {
//Do something
}
});
You should try like this....
var checkboxes = $("input[type='checkbox']"),
submitButt = $("input[type='submit']");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
you need to check if checkbox is checked or not.
$("#select_all").click(function(){
var checkboxes = $("input[type='checkbox']");
if(checkboxes.is(":checked"))
alert("checked");
else
alert("select at least one;
});