How can I add and remove the attribute of the add button to the row if there are already 10 rows on the page? I'm already confused and the logic here is stupid. How can I manage with less code and delegation?

var addplace = function placeHold() { $('.matrix_a tr').each(function(i,v){//loop each row $(v).find('td').each(function(x,d){//loop each colon in that row $(d).find('input').attr('placeholder','a'+(i+1)+','+(x+1));//change the input placeholder }) }) }; var addplaceh = function placeHold() { $('.matrix_b tr').each(function(i,v){//loop each row $(v).find('td').each(function(x,d){//loop each colon in that row $(d).find('input').attr('placeholder','b'+(i+1)+','+(x+1));//change the input placeholder }) }) }; $('.add_str').click(function(){ if ($("#check_mtrx_a").prop("checked")){ $('.matrix_a tr:first').clone().appendTo('.matrix_a').add(addplace); }else if($("#check_mtrx_b").prop("checked")) { $('.matrix_b tr:first').clone().appendTo('.matrix_b').add(addplaceh); } }); if ($('.matrix_a tr td').length > 4) { $('.del_str').removeAttr("disabled").click(function(){ if ($("#check_mtrx_a").prop("checked")){ $('.matrix_a tr:last').remove(); }else if($("#check_mtrx_b").prop("checked")) { $('.matrix_b tr:last').remove(); } }); } $('.add_col').click(function(){ if ($("#check_mtrx_a").prop("checked")){ $('.matrix_a tr td:last').clone().appendTo('.matrix_a tr').add(addplace); }else if($("#check_mtrx_b").prop("checked")) { $('.matrix_b tr td:last').clone().appendTo('.matrix_b tr').add(addplaceh); } }); if ($('.matrix_a tr').length > 2) { $('.del_col').removeAttr("disabled").click(function(){ if ($("#check_mtrx_a").prop("checked")){ $('.matrix_a tr td:last-child').remove(); }else if($("#check_mtrx_b").prop("checked")) { $('.matrix_b tr td:last-child').remove(); } }); } $(document).on('click' , '.add_str', function() { if ($("#check_mtrx_a").prop("checked")){ if ($('.matrix_a tr').length === 10){ $('.add_str').attr('disabled', 'disabled'); } } if ($("#check_mtrx_b").prop("checked")){ if ($('.matrix_b tr').length === 10){ $('.add_str').attr('disabled', 'disabled'); } } }); $(document).on('click' , '.del_str', function() { if ($("#check_mtrx_a").prop("checked")){ if ($('.matrix_a tr').length <10){ $('.add_str').removeAttr('disabled'); } } if ($("#check_mtrx_b").prop("checked")){ if ($('.matrix_b tr').length <10){ $('.add_str').removeAttr('disabled'); } } }); $(document).on('click' , '.add_col', function() { if ($("#check_mtrx_a").prop("checked")){ if ($('.matrix_a tr:first td').length >= 10){ $('.add_col').attr('disabled', 'disabled'); } } if ($("#check_mtrx_b").prop("checked")){ $(".add_col").removeAttr('disabled') if ($('.matrix_b tr:first td').length === 10){ $('.add_col').attr('disabled', 'disabled'); } } }); $(document).on('click' , '.del_col', function() { if ($("#check_mtrx_a").prop("checked")){ if ($('.matrix_a tr td').length <10){ $('.add_col').removeAttr('disabled'); } } if ($("#check_mtrx_b").prop("checked")){ if ($('.matrix_b tr:first-child td').length <10){ $('.add_col').removeAttr('disabled'); } } }); 
 .matrix_a_cover { float: left; } .matrix_b_cover { float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="add_mtrx"> <span class="rad btn_a"> <label for="matrix_a"> <input checked="checked" name="mtrx_sel" type="radio" value="mtrx_a" id="check_mtrx_a">Матрица А </label> </span> <span class="rad btn_b"> <label for="matrix_b"> <input type="radio" name="mtrx_sel" value="mtrx_b" id="check_mtrx_b">Матрица В </label> </span> <br> <div class="add_del_btns"> <button class="add_str btn-style" type="button"><i class="fa fa-plus" aria-hidden="true"></i>Добавить</button> <button class="del_str btn-style" type="button" ><i class="fa fa-minus" aria-hidden="true"></i>Удалить</button>строку <br> <button class="add_col btn-style" type="button"><i class="fa fa-plus" aria-hidden="true"></i>Добавить</button> <button class="del_col btn-style" type="button" ><i class="fa fa-minus" aria-hidden="true"></i>Удалить</button>столбец </div> </div> <div class="matrix_a_cover"> <table class="matrix_a brackets" id="matrix_a"> <tr> <td class="str_inp"> <input type="text" placeholder="a1,1"> </td> <td class="str_inp"> <input type="text" placeholder="a1,2"> </td> </tr> <tr> <td class="str_inp"> <input type="text" placeholder="a2,1"> </td> <td class="str_inp"> <input type="text" placeholder="a2,2"> </td> </tr> <tr> <td class="str_inp"> <input type="text" placeholder="a3,1"> </td> <td class="str_inp"> <input type="text" placeholder="a3,2"> </td> </tr> <tr> <td class="str_inp"> <input type="text" placeholder="a4,1"> </td> <td class="str_inp"> <input type="text" placeholder="a4,2"> </td> </tr> </table> </div> <div class="matrix_b_cover"> <table class="matrix_b brackets" id="matrix_b"> <tr> <td class="str_inp"> <input type="text" placeholder="b1,1"> </td> <td class="str_inp"> <input type="text" placeholder="b1,2"> </td> <td class="str_inp"> <input type="text" placeholder="b1,3"> </td> </tr> 

  • if (length of the string selection> = 10) {execute the disabled button} - Jean-Claude
  • now it’s worth it, but the logic is stupid, because delegation on a click is worth it and doesn’t work when changing the checkbox. reset attribute - Drop

1 answer 1

I ruined you the compression of the code, made the button handlers to add a row / column, to finish processing the buttons to remove the row / column by analogy with the first two buttons. input value changed to the classes of forms for which you select the matrix.

You have unclosed tags, this is wrong.

 $(function() { //пользоват.фу-ция var addplace = function placeHold() { matrix.find('tr').each(function(i, v) { //loop each row $(v).find('td').each(function(x, d) { //loop each colon in that row $(d).find('input').attr('placeholder', matrixID.split('_')[1] + (i + 1) + ',' + (x + 1)); }) }) }; // начальные значения var matrixID = $('input[name=mtrx_sel]:checked').val(); var matrix = $('.' + matrixID); $('input[name=mtrx_sel]').on('change', function() { matrixID = $(this).val(); matrix = $('.' + matrixID); checkAddStr(); checkAddCol(); }); //обработка кнопок добавить $('.add_str').click(function() { matrix.find('tr:first').clone().appendTo(matrix).add(addplace); checkAddStr(); }); $('.add_col').click(function() { matrix.find('tr td:last').clone().appendTo(matrix.find('tr')).add(addplace); checkAddCol(); }); //проверка активности кнопки доб.стр. function checkAddStr() { if ($("input[value=" + matrixID + "]").prop("checked")) { if (matrix.find('tr').length >= 10) { $('.add_str').prop('disabled', true); } else { $('.add_str').prop('disabled', false); } } } //доб.колон. function checkAddCol() { if ($("input[value=" + matrixID + "]").prop("checked")) { if (matrix.find('tr:first td').length >= 10) { $('.add_col').prop('disabled', true); } else { $('.add_col').prop('disabled', false); } } } //по аналогии с проверкой пред. кнопок сделаете дальше // $(document).on('click', '.del_str', function() { // if ($("#check_mtrx_a").prop("checked")) { // if ($('.matrix_a tr').length < 10) { // $('.add_str').removeAttr('disabled'); // } // } // if ($("#check_mtrx_b").prop("checked")) { // if ($('.matrix_b tr').length < 10) { // $('.add_str').removeAttr('disabled'); // } // } // }); // $(document).on('click', '.del_col', function() { // if ($("#check_mtrx_a").prop("checked")) { // if ($('.matrix_a tr td').length < 10) { // $('.add_col').removeAttr('disabled'); // } // } // if ($("#check_mtrx_b").prop("checked")) { // if ($('.matrix_b tr:first-child td').length < 10) { // $('.add_col').removeAttr('disabled'); // } // } // }); }); 
 .matrix_b_cover { margin-top: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="add_mtrx"> <span class="rad btn_a"> <label for="matrix_a"> <input checked="checked" name="mtrx_sel" type="radio" value="matrix_a" id="check_mtrx_a">Матрица А </label> </span> <span class="rad btn_b"> <label for="matrix_b"> <input type="radio" name="mtrx_sel" value="matrix_b" id="check_mtrx_b">Матрица В </label> </span> <br> <div class="add_del_btns"> <button class="add_str btn-style" type="button"><i class="fa fa-plus" aria-hidden="true"></i>Добавить</button> <button class="del_str btn-style" type="button"><i class="fa fa-minus" aria-hidden="true"></i>Удалить</button>строку <br> <button class="add_col btn-style" type="button"><i class="fa fa-plus" aria-hidden="true"></i>Добавить</button> <button class="del_col btn-style" type="button"><i class="fa fa-minus" aria-hidden="true"></i>Удалить</button>столбец </div> </div> <div class="matrix_a_cover"> <table class="matrix_a brackets" id="matrix_a"> <tr> <td class="str_inp"> <input type="text" placeholder="a1,1"> </td> <td class="str_inp"> <input type="text" placeholder="a1,2"> </td> </tr> </table> </div> <div class="matrix_b_cover"> <table class="matrix_b brackets" id="matrix_b"> <tr> <td class="str_inp"> <input type="text" placeholder="b1,1"> </td> <td class="str_inp"> <input type="text" placeholder="b1,2"> </td> </tr> </table> </div> 

  • Apparently when copying, not all closing copies were copied. Thanks a lot, the code has become much smaller! - Drop
  • In theory, is it looking for a table through checkboxes? And how do I understand the addition buttons of rows and columns universal, under any selected matrix? If the buttons are vertical, then how to deal with 2 placeholder variables for the b matrix? I can't understand yet why adding rows to my code does not work and columns - Drop
  • I found everything in my radio value the value was different. - Drop
  • Well, in general, there is only a problem with the placeholder for the matrix b - Drop
  • @Drop updated answer and addplace () function - Jean-Claude