¿Hay una función prefabricada en jQuery en la que cuando hace clic en un botón habilitará una fila? Luego, si vuelve a hacer clic en él, desactivará la fila. Encontré sobre toggle() y toggleClass(). Pero funciona cuando llama a una función. El pensamiento es algo así, pero sé que la sintaxis es incorrecta.
jQuery
function disableRows(){ $("#test1table tbody tr").each(function() { $(this).find('input:text').prop('disabled', true); }); function enableRows(){ $("#test1table tbody tr").each(function() { $(this).find('input:text').prop('disabled', false); }); $(document).ready(function() { $('.yearButt').click(function(){ $(this).toggleClass(enableRows(), disableRows()); }); });Creo que el siguiente código lo ayuda a comprender los requisitos:
Ref: Habilitar Deshabilitar controles en una fila de la tabla
$(document).on('change', '.chkView', function() { $(this).closest('tr').find('.chkEdit, .chkDelete').prop('disabled', !this.checked); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table_forms"> <tr> <td><input type="checkbox" class="chkView"/>View</td> <td><input type="checkbox" class="chkEdit" disabled/>Edit</td> <td><input type="checkbox" class="chkDelete" disabled/>Delete</td> </tr> <tr> <td><input type="checkbox" class="chkView"/>View</td> <td><input type="checkbox" class="chkEdit" disabled/>Edit</td> <td><input type="checkbox" class="chkDelete" disabled/>Delete</td> </tr> </table>Puedes simplificar tu código:
$(document).ready(function() { $('.yearButt').click(function(){ var enabled = parseInt($(this).data('enabled')); $("#test1table tbody tr input:text").prop('disabled', enabled ); $(this).data('enabled', !enabled) }); });Use alternar , no alternar Clase, alternar llamará a funciones alternativamente en cada evento de clic
$(document).ready(function() { $('.yearButt').toggle( function() { enableRows(); }, function() { disableRows(); } ); }); });También puede optimizar su código de habilitación y deshabilitación eliminando bucles y usando los selectores adecuados para el elemento deseado
$("#test1table").find("input,button,textarea,select").attr("disabled", "disabled");
o esto