abpetkov / switchery

iOS 7 style switches for your checkboxes
http://abpetkov.github.io/switchery/
2.06k stars 477 forks source link

Send via Ajax at the click? #154

Open OursEnSki opened 6 years ago

OursEnSki commented 6 years ago

Hello

I'm looking for just sent ajax at the time of "Change", it works, but it sends twice the data ...

If I do : $ ( '# SuivreGeo'). Change (function () { ... $ .ajax ({ method: 'POST', ... It seems to me that at the moment of "change" it launches two actions giving the current state, then the state changed.

If I put "click" instead of "change" it sends once the information, but in this case the switch status no longer changes :(

How to send directly to Ajax when clicking on the Switch?

Thanks for your help. Have a good day

tsuijie commented 6 years ago

Try this:

$ ( '# SuivreGeo'). Change (function () {
    if ($(this).attr('disabled) == 'disabled') return false;
    $(this).attr('disabled', 'disabled');
    $.ajax({
      ...
    }).complete(function () {
      $(this).removeAttr('disabled')
    })
})