JQuery: событие onclick работает только в первый разJquery

Программирование на jquery
Anonymous
JQuery: событие onclick работает только в первый раз

Сообщение Anonymous »

Пытаемся добавить два обработчика событий для двух элементов соответственно. Второй элемент появится в DOM только после того, как пользователи щелкнут первый элемент. Моя проблема в том, что первый обработчик событий работает только один раз, второй обработчик событий не работает вообще. Здесь хотелось бы получить совет от специалистов. Спасибо.
HTML: (до нажатия первой кнопки)


Per person
$ 100
Add protection



HTML: (после нажатия первой кнопки)


Per person
$100

Added


Remove
protection





jQuery('#ribe-purc-add-insurance').on('click', () => {
if (
jQuery('.ribe-purc-insurance-remove-text').length === 1 &&
jQuery('.ribe-service-insurance-chubb-logo.optClass').length === 0
) {
jQuery('.ribe-purc-insurance-remove-text').text(removeInsC);
jQuery('.ribe-service-insurance-chubb-logo').addClass('optClass');
console.log('addedClass + removeInsC');

// Use event delegation to attach the event handler
jQuery('body').on('click', '.ribe-purc-insurance-remove-text', function () {
if (jQuery('.ribe-service-insurance-chubb-logo.optClass').length === 1) {
jQuery('.ribe-service-insurance-chubb-logo.optClass').removeClass(
'optClass',
);
}
});
}
});


Подробнее здесь: https://stackoverflow.com/questions/787 ... first-time

Вернуться в «Jquery»