Кнопка «Выбрать / Отменить выбор всех кнопок» не работает - PullRequest
1 голос
/ 08 июля 2019

У меня есть код ниже, где я пытаюсь установить флажок выбора / отмены выбора в.

Флажки находятся внутри тела таблицы. Я не могу понять, почему чекбоксы не выбираются / не снимаются. Но значение кнопки выбора / отмены изменяет свое значение.

Проверено на Chrome 75

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Add / Remove Items Rows Dynamically</title>
<style type="text/css">
    form{
        margin: 20px 0;
    }
    form input, button{
        padding: 5px;
    }
    table{
        width: 100%;
        margin-bottom: 20px;
        border-collapse: collapse;
    }
    table, th, td{
        border: 1px solid #cdcdcd;
    }
    table th, table td{
        padding: 10px;
        text-align: left;
    }
</style>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".add-row").click(function(){
            var name = $("#name").val();
            var qty = $("#qty").val();
            var markup = "<tr><td><input type='checkbox' class='checkBoxClass' name='record'></td><td>" + name + "</td><td>" + qty + "</td></tr>";
            $("table tbody").append(markup);
        });

        // remove selected table rows
        $(".delete-row").click(function(){
            $("table tbody").find('input[name="record"]').each(function(){
                if($(this).is(":checked")){
                    $(this).parents("tr").remove();
                }
            });
        });
        //submit button to implement
        $(".submit-row").click(function(){
            alert("items submitted");
            $("table tbody").find('input[name="record"]').each(function(){
                if($(this).is(":checked")){
                    $(this).parents("tr").remove();
                }
            });
        });

    //Implemented code, but does not work
    $(".checkAll").click(function() {
        if ($(this).val() == 'Check All') {
            $('.checkBoxClass input').prop('checked', true);
            $(this).val('Uncheck All');
        } else {
            $('.checkBoxClass input').prop('checked', false);
            $(this).val('Check All');
        }
    });

    });    
</script>
</head>
<body>
    <form>
        <input type="text" id="name" placeholder="Name">
        <input type="number" id="qty" placeholder="Quantity">
        <input type="button" class="add-row" value="Add Row">
    </form>
    <table id="tab1">
        <thead>
            <tr>
                <th>Select</th>
                <th>Name of the item</th>
                <th>Qty</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <button type="button" class="delete-row">Delete Row</button>
    <button type="button" class="submit-row">Submit selected Rows</button>
    <input type="button" id="checkAll" class="checkAll"  value="Check All" />
</body> 
</html>                            

1 Ответ

2 голосов
/ 08 июля 2019

Это потому, что ваш селектор не выбирает входы. Сделайте это так.

$(".checkAll").click(function() {
    if ($(this).val() == 'Check All') {
        $('input.checkBoxClass').prop('checked', true);
        $(this).val('Uncheck All');
    } else {
        $('input.checkBoxClass').prop('checked', false);
        $(this).val('Check All');
    }
});

Когда вы говорите

$('.checkBoxClass input')

, что означает «Выбрать все входы, которые находятся внутри классифицированного элемента checkBoxClass».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...