mardi 29 mai 2018

disable/enable button when checkboxes is not checked

I have a MVC application in C# and in a view that will have some checkboxes and on this list of checkboxes some checkboxes will come "checked and disable" from the database depends if I pass some or other value of ID.
But my problem is my button only can submit if i check a checkbox enable or if i have more than 2 enable and checked all the checkboxes enabled and not checked the checkbox disabled.

   <input type="checkbox" name="SelectAll" id="checkboxPrincipal" />SelectALL


@foreach (var item in Model)
    {

     @if (codigoUniversidad != -1) {
                UniversalTitulosEntities db = UniversalTitulos.App_Start.ConexionConfig.db(codigoUniversidad);
                TbEstadoLoteEST consulta = db.TbEstadoLoteEST.Where(e => e.IdEstadoLoteEST == item.EstadoLOT).SingleOrDefault();
                if (consulta.IdEstadoLoteEST == 7) {
                        <td><input type="checkbox" value="@item.LoteIdLOT" name="mycheckbox" id="mycheckbox"/></td>
                }
                if (consulta.IdEstadoLoteEST == 8) {
                        <td><input type="checkbox"  value="@item.LoteIdLOT" name="mycheckbox" id="mycheckbox" checked="checked" disabled="disabled"/></td>
                }
                if (consulta.IdEstadoLoteEST != 8 && consulta.IdEstadoLoteEST != 7) {
                        <td><input type="checkbox" value="@item.LoteIdLOT" name="mycheckbox" id="mycheckbox" disabled="disabled"/></td>
                }
            }



 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="myButton" name="myButton" >Update </button>


<script type="text/javascript">
        $(document).ready(function () {
            $('input[name=checkboxPrincipal]').click(function () {
                $('input[name=mycheckbox]:enabled').prop('checked', $(this).prop('checked'));
            });
            $('#myButton').click(function () {
                var listaIdLotes = [];
                $('#mycheckbox:checked').each(function () {
                    listaIdLotes.push(this.value);

             });

                $(function () {
                    var checkboxes = $(':checkbox:not(#checkboxPrincipal)').click(function (event) {
                        $('#Actualizar').prop("disabled", checkboxes.filter(':checked').length == 0);


                    });

                    $('#checkboxPrincipal').click(function (event) {
                        checkboxes.prop('checked', this.checked);
                        $('#Actualizar').prop("disabled", !this.checked)

                    });
                    $('input[type=checkbox]').each(function () {
                        var elem = $("#" + this.id);
                        if (!elem.attr("disabled")) {
                            this.checked = true;
                        }
                    });
                });




Aucun commentaire:

Enregistrer un commentaire