Измените значение ввода в родном скрытом TD - PullRequest
0 голосов
/ 11 февраля 2020

Когда моя кнопка удаления нажата, я хотел бы изменить значение входа is_active, имеющего класс 'active', с «yes» на «no». До сих пор я пытался использовать братьев и сестер, чтобы попытаться выбрать вход с классом «активный». Не повезло.

Вот мой код:

<script type="text/javascript">
$(document).on("click", ".delete", function(){

         if ($(this).parents('.idcheck').length){

        $(this).siblings("td").next().find('.active').val('no'); 
        $(this).parents("tr").hide();

         }
        else 
        {$(this).parents("tr").remove();
        }

        $(".add-new").removeAttr("disabled");

    });
</script>  

<table class="table table-bordered">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>County</th>
            <th>Street</th>
            <th>City</th>
            <th>Ward</th>
            <th>Precinct</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    <form id="" class="form" name="parts" method="post" action="">
    <tr class="idcheck">
        <td style="display:none;"><input type="hidden" class="form-control" name="username" id="username" value="user"></td>
        <td><input type="text" class="form-control" name="first_name" id="first_name" value="John"></td>
        <td><input type="text" class="form-control" name="last_name" id="last_name" value="Doe"></td>
        <td><input type="text" class="form-control" name="county" id="county" value="my county"></td>
        <td><input type="text" class="form-control" name="street" id="street" value="456 easy street"></td>
        <td><input type="text" class="form-control" name="city" id="city" value="town"></td>
        <td><input type="text" class="form-control" name="ward" id="ward" value="5"></td>
        <td><input type="text" class="form-control" name="precinct" id="precinct" value="2"></td>
        <td style="display:none;"><input type="hidden" class="form-control" name="id" id="id" value="1"></td>
        <td style="display:none;"><input type="hidden" class="form-control active" name="is_active" id="is_active" value="yes"></td>
        <td><a class="delete" title="" data-toggle="tooltip" data-original-title="Delete"><i class="material-icons"></i></a>
        </td>
    </tr>
    </tbody>
</table>

1 Ответ

1 голос
/ 12 февраля 2020

Я понял, вот обновленный код:

<script type="text/javascript">
$(document).on("click", ".delete", function(){

         if ($(this).parents('.idcheck').length){

        $(this).closest('tr').find('.active').val('no'); 
        $(this).parents("tr").hide();

         }
        else 
        {$(this).parents("tr").remove();
        }

        $(".add-new").removeAttr("disabled");

    });
</script>  

<table class="table table-bordered">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>County</th>
            <th>Street</th>
            <th>City</th>
            <th>Ward</th>
            <th>Precinct</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    <form id="" class="form" name="parts" method="post" action="">
    <tr class="idcheck">
        <td style="display:none;"><input type="hidden" class="form-control" name="username" id="username" value="user"></td>
        <td><input type="text" class="form-control" name="first_name" id="first_name" value="John"></td>
        <td><input type="text" class="form-control" name="last_name" id="last_name" value="Doe"></td>
        <td><input type="text" class="form-control" name="county" id="county" value="my county"></td>
        <td><input type="text" class="form-control" name="street" id="street" value="456 easy street"></td>
        <td><input type="text" class="form-control" name="city" id="city" value="town"></td>
        <td><input type="text" class="form-control" name="ward" id="ward" value="5"></td>
        <td><input type="text" class="form-control" name="precinct" id="precinct" value="2"></td>
        <td style="display:none;"><input type="hidden" class="form-control" name="id" id="id" value="1"></td>
        <td style="display:none;"><input type="hidden" class="form-control active" name="is_active" id="is_active" value="yes"></td>
        <td><a class="delete" title="" data-toggle="tooltip" data-original-title="Delete"><i class="material-icons"></i></a>
        </td>
    </tr>
    </tbody>
</table>

Я изменил строку с:

$(this).siblings("td").next().find('.active').val('no');

На это:

$(this).closest('tr').find('.active').val('no');
...