-4

I upgrade my jQuery library to 1.9.1 and have this code:

if (
    !($("#Days_Sat").attr("checked"))
    && !($("#Days_Sun").attr("checked"))
    && !($("#Days_Mon").attr("checked"))
    && !($("#Days_Tu").attr("checked"))
    && !($("#Days_We").attr("checked"))
    && !($("#Days_Tr").attr("checked"))
    && !($("#Days_Fr").attr("checked"))) {
         alert("pleas select day");
            }

but even i select day checkbox the alert shown!

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30

9 Answers9

2

You can use is(':checked') to know the checkbox state, this will return true if checkbox is checked otherwise false. see below code

if (!($("#Days_Sat").is(':checked'))
    && !($("#Days_Sun").is(':checked'))
    && !($("#Days_Mon").is(':checked'))
    && !($("#Days_Tu").is(':checked'))
    && !($("#Days_We").is(':checked'))
    && !($("#Days_Tr").is(':checked'))
    && !($("#Days_Fr").is(':checked'))) 
   {
      alert("pleas select day");
   }

If you have all your concerned checkbox id starting with Days then you can use jQuery start with selector to get all checkbox with id starting from 'Days_' and get the length of object to find if none of them is selected. see below code

var checked = $('input[id^="Days_"]:checked').length;
if(checked==0)
  alert("pleas select day");
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0

Try

$('##Days_Sun').is(':checked');
0

You can use .is() method with :checked selector. like this:

$("#Days_Fr").is(":checked")
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

I would check if there is any ID starting with "Days_" that is checked. If not (!) the if clause will be thruthy.

if (!$("[id^='Days_']:checked").length) alert("pleas select day");

$('button').click(function(){
    if (!$("[id^='Days_']:checked").length) alert('please choose a day...');
    else alert('cool...');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="checkbox" id="Days_Sat" />
<input type="checkbox" id="Days_Sun" />
<input type="checkbox" id="Days_Mon" />
<input type="checkbox" id="Days_Tu" />
<input type="checkbox" id="Days_We" />
<input type="checkbox" id="Days_Tr" />
<input type="checkbox" id="Days_Fr" />
<button type="button">Test</button>
Rikard
  • 7,485
  • 11
  • 55
  • 92
0

To make your code easiers to read i would go for:

if ( $('#Days_Sat, #Days_Sun, #Days_Mon, #Days_Tu, #Days_We, #Days_Tr, #Days_Fr').filter(':checked').length != 1 )
{

}

Other than that: like the other people already answered: .is(':checked') is your solution here.

DoXicK
  • 4,784
  • 24
  • 22
0

Check like this

if(!$("[id^='Days_']").is(':checked')){

         alert("pleas select day");
  }
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
0

$('#your_id').is(':checked') gives you either check box is checked or not. It returns true if it is checked or false when not checked

  if($('#Days_Sat').is(':checked'))
    {
    do something...
    }

and you full code

if(!$('#Days_Sat').is(':checked')&&!$('#Days_Sun').is(':checked')&&!$('#Days_Mo').is(':checked')&&!$('#Days_Tu').is(':checked')&&!$('#Days_Th').is(':checked')&&!$('#Days_Fr').is(':checked'))
{
alert("pleas select day");
}
Rasel
  • 5,488
  • 3
  • 30
  • 39
0

Use jQuery is method instead of attr.

jQuery API link.

Demo

aulizko
  • 179
  • 4
0

Look at the jsfiddle here JsFiddle

$(function(){

    $('#check').click(function(){
        if($("[id^='days_']:checked").length !=7 ){
        alert("Please select a day");}
        else{alert("works fine");}
    });

  });

This will popup the alert if any checkboxe is not clicked.

if you want that atleast one should be selected you can do

if($("[id^='days_']:checked").length !=0 )
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23