2015-08-26 3 views
5

Я Использование JavaScript оповещения библиотеки sweetalertsweetalert: как пройти аргумент обратного вызова

Мой код:

function foo(id) { 
    swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this imaginary file!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }, 
    function(){ 
     swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
    }); 
} 

Как я могу передать id от foo() функции обратного вызова функции в swal?

ответ

6
function(id){ 
    alert(MyId); 
    swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
}); 

Это не будет работать, потому что в этом случае ид это опция isConfirm для вашего диалогового окна подтверждения - см. SweetAlert Documentation.

Это не будет работать - нет необходимости в дополнительной переменной:

function foo(id) { 
    swal({ 
    title: "Are you sure?", 
    text: "You will not be able to recover this imaginary file!", 
    type: "warning", 
    showCancelButton: true, 
    confirmButtonColor: "#DD6B55", 
    confirmButtonText: "Yes, delete it!", 
    closeOnConfirm: false 
    }, 
    function(isConfirm){ 
    alert(isConfirm); 
    alert(id); 
    swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
    }); 
} 
foo(10); 

здесь jsfiddle: http://jsfiddle.net/60bLyy2k/

2

просто положить параметр в локальной переменной, они доступны во внутренней функции или в clousers

function foo(id) { 
    var MyId = id; 
    swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this imaginary file!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }, 
    function(){ 
     alert(MyId); 
     swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
    }); 
} 

foo(10); 

здесь скрипка https://jsfiddle.net/

+0

почему проезжают его в 'функции (ID) {' ?? вы можете напрямую обращаться к 'MyId' – ozil

+0

, ваш' скрипка' тоже не работает – ozil

+0

во внутренней функции (id) {нет необходимости здесь здесь id, поэтому я удалил это, а также исправил URL скрипта –

Смежные вопросы