2015-12-02 1 views
1

Я создал окно ввода, когда я подтверждаю его отправку значения и получение предупреждения об успешном завершении, но я тоже получаю успех на кнопке отмены?Ajax работает в обоих случаях

swal({ 
    title: " Asking price for " + askData[0] + "?", 
    text: "If you are sure press OK or edit this text", 
    type: "input", 
    inputType: "text", 
    inputValue: "Hello " + askData[1] + ", I am interested in buying your " + askData[0] + " of Variety :" + askData[3] + ". Please update the Price!", 
    showCancelButton: true, 
    closeOnConfirm: false, 
    showLoaderOnConfirm: true 
}, function (inputValue) { 
    if (inputValue === "") { 
     swal.showInputError("Please write something !"); 
     return false; 
    } 
    $.ajax({ 
     url: urlEnq, 
     type: "GET" 
    }); 
    $.ajax({ 
     url: 'saveUserMessage', 
     type: "POST", 
     data: { 
      fieldUserId: fieldUserId, 
      filedUserCompanyId: filedUserCompanyId, 
      message: inputValue 
     }, 

    }) 
     .done(function (data) { 
      swal("Done!", "Your message has been successfully sent.", "success"); 
     }) 
     .error(function (data) { 
      swal.showInputError("Please write something !"); 
     }); 
+3

что значение 'переменной inputValue' при нажатии кнопки отмены? –

+0

inputValue используется для поля ввода значения, текстовое поле. –

+0

Да, мы знаем, что, каково фактическое значение переменной? Вы делаете if, если на этой переменной, чтобы решить, нужно ли вам делать вызов ajax –

ответ

1

При использовании SweetAlert значение переменной, переданной в функцию обратного вызова будет false и не '' при нажатии отменить.

Я обновил ваш jsfiddle, чтобы показать это, смотрите обновленный код ниже:

$('button.delete-account').click(function (e) { 
    swal({ 
     title: "Are you sure you want to delete your account?", 
     text: "If you are sure, type in your password:", 
     type: "input", 
     inputType: "password", 
     showCancelButton: true, 
     closeOnConfirm: false 
    }, function (typedPassword) { 
     if (typedPassword === false) { 
      return; 
     } 
     if (typedPassword === "") { 
      swal.showInputError("You need to type in your password in order to do this!"); 
      return false; 
     } 
     $.ajax({ 
      url: "/api/delete-account", 
      data: { password: typedPassword }, 
      type: "POST" 
     }).done(function (data) { 
      swal("Deleted!", "Your account has been deleted!", "success"); 
     }).error(function (data) { 
      swal.showInputError("Your password is wrong!"); 
     }); 
    }); 
}); 
+0

Спасибо, Теперь. –