Я создаю плагин для массового добавления страниц в WP, но мне нужно удалять строки по мере необходимости. Попытка удалить строку каждый раз при нажатии кнопки удаления, но она не срабатывает. Что я делаю не так?
jQuery(document).ready(function($) {
// Add new page entry
$('#tz-bpc-add-page-entry').click(function() {
var index = $('#tz-bpc-page-entries-table tbody tr').length;
// Clone the first row
var newRow = $('#tz-bpc-page-entries-table tbody tr').first().clone();
// Clear input values in the cloned row
newRow.find('input').val('');
newRow.find('select').prop('selectedIndex', 0);
// Update the "name" attributes to use the new index
newRow.find('input, select').each(function() {
var name = $(this).attr('name');
name = name.replace(/\[\d\]/, '[' + index + ']'); // Replace the index
$(this).attr('name', name);
});
// Append row
$('#tz-bpc-page-entries-table tbody').append(newRow);
// Bind click to the new delete button
newRow.find('.tz-bpc-delete-row').click(function(e) {
e.preventDefault(); // Prevent default button behavior
var row = $(this).closest('tr');
// Check if the row is the first row and prevent deletion
if (row.index() === 0) {
alert('The first row cannot be deleted.');
return; // Stop the deletion
}
// If not the first row, delete the row
row.remove();
});
});
});
Я создаю плагин для массового добавления страниц в WP, но мне нужно удалять строки по мере необходимости. Попытка удалить строку каждый раз при нажатии кнопки удаления, но она не срабатывает. Что я делаю не так?
[code]jQuery(document).ready(function($) { // Add new page entry $('#tz-bpc-add-page-entry').click(function() { var index = $('#tz-bpc-page-entries-table tbody tr').length;
// Clone the first row var newRow = $('#tz-bpc-page-entries-table tbody tr').first().clone();
// Clear input values in the cloned row newRow.find('input').val(''); newRow.find('select').prop('selectedIndex', 0);
// Update the "name" attributes to use the new index newRow.find('input, select').each(function() { var name = $(this).attr('name'); name = name.replace(/\[\d\]/, '[' + index + ']'); // Replace the index $(this).attr('name', name); });
// Bind click to the new delete button newRow.find('.tz-bpc-delete-row').click(function(e) { e.preventDefault(); // Prevent default button behavior var row = $(this).closest('tr');
// Check if the row is the first row and prevent deletion if (row.index() === 0) { alert('The first row cannot be deleted.'); return; // Stop the deletion }
// If not the first row, delete the row row.remove(); }); }); });[/code] [code] Add row
Page Title Page Status Author Page Template Actions