jeudi 19 novembre 2015

jQuery check if checkbox is unchecked not working

I got some problem while checking checkbox, it always return true, didn't work in the false statement, I want to stop before click the add button if none of the checkbox is checked. How to fix it?

Here's my code:

$('.add').click(function() {
        var weekDays = [];
        $('#checkBox :checked').each(function () {
                weekDays.push($(this).attr('name'));
        });
        
    var row = '<tr>'
                        + '<td class="rowDays">' + weekDays + '</td>'
                        + '</tr>';
        
    if (!$("#days").find("checkbox").is(":checked")) {
                $(row).insertAfter($('#days > tbody > tr:last'));
        } else{
        alert('unchecked!')
        return false;
    }
});
<script src="http://ift.tt/Zv5K7g"></script>
<table id="checkBox">
    <tbody>
            <tr>
                    <th>Sun</th>
                        <th>Mon</th>
                        <th>Tue</th>
                        <th>Wed</th>
                        <th>Thu</th>
                        <th>Fri</th>
                        <th>Sat</th>
        </tr>
                <tr>
                        <td><input name="Sunday" type="checkbox" value="0"></td>
                        <td><input name="Monday" type="checkbox" value="1"></td>
                        <td><input name="Tuesday" type="checkbox" value="2"></td>
                        <td><input name="Wednesday" type="checkbox" value="3"></td>
                        <td><input name="Thursday" type="checkbox" value="4"></td>
                        <td><input name="Friday" type="checkbox" value="5"></td>
                        <td><input name="Saturday" type="checkbox" value="6"></td>
                </tr>
        </tbody>
</table>
            
<button type="button" class="add">Add Days</button>
<br>
<br>
<table id="days">
    <tbody>
                <tr>
                        <th>Days</th>
                </tr>
        </tbody>
</table>

JSFiddle




Aucun commentaire:

Enregistrer un commentaire