+1 vote
by
There is a form for enabling sms notifications. There is a button 'on' with the class 'on', when you click on it sent AJAX request, and if the answer is positive, then the button class must change to 'off', and hang another click handler, already for the class 'off' with another AJAX request to turn off, respectively. I did it like this:

$('.on').click(function(e){
e.preventDefault();

getData('', 'POST', $('.sms-not-form').serialize()).then(result=>{
let on=result;

if (on.code=='0') {
$('.change-password').find('input').remove();
getData('', 'POST').then(result=>{
let userinfo=result.data.uinfo;
if(result.code=='0') {
$('.change-password').append('<input name="mphone" readonly value="'+userinfo.sms.phone+'">');
}
})
$('.status').find('.sms-not-button-on').attr('value','Выключить');
$('.status').find('.not-status').html('Включено');
$('.status').find('.sms-not-button-on').attr('class','sms-not-button-off');
}

});
$('.off').click(function(e){
e.preventDefault();

getData('', 'POST', $('.sms-not-form').serialize()).then(result=>{
let off=result;

if (off.code=='0') {
$('.status').find('.sms-not-button-off').attr('class','sms-not-button-on');
$('.status').find('.sms-not-button-on').attr('value','Включить');
$('.change-password').find('input').mask("+38 (072)-999-99-99");
$('.status').find('.not-status').html('Выключено');
}

});
});
})
GetData is an AJAX request. There by clicking on Enable, after the request button changes to Disable, without reloading, the value itself changes and appears Disable, as well as changing the class to off, but if I then click on Disable, then a second click with the request does not work. How can this be corrected?

2 Answers

0 votes
by
I would solve it like this:

$(function() {
$('.on').click(function(e) {
e.preventDefault();

if ($(this).hasClass('off')) {
$(this).removeClass('off');
return // тут запрос на отключение
}

$(this).addClass('off');

return // тут запрос на подключение
});
});
0 votes
by
Try it instead of
$('.on').click(function(e){});
use
$(".on").on("click", function(e){});
or
$(document).on("click", ".on", function(e){});
...