2014-03-01 3 views
1

Я изучаю jQuery самостоятельно и нуждаюсь в небольшой помощи здесь, пожалуйста.jQuery - простая глобальная переменная

Я не знаю, почему, но переменная «msg» в этом коде ниже подходит к концу в конце.

jQuery(function($) { 
    $('#mailing').submit(function(e) { 
    e.preventDefault(); 

    var msg = ''; 

    $.ajax({ 
     type: "POST", 
     url: "ajx_mailing.asp", 
     data: '[email protected]', 
     cache: false, 
     dataType: "text", 
     complete: function(data){ 
     if (data.responseText = 'ok') { 
      msg = 'The e-mail was included with success!'; 
     } else { 
      msg = 'There is a problem, please check the e-mail!'; 
     } 
     }, 
     error: function(data) { 
     msg = 'Error! Please, try again latter!'; 
     }, 
    }); 

    /* http://www.ericmmartin.com/projects/simplemodal/ */ 

    $('#simplemodal').modal({ 
     minWidth: 410, 
     minHeight: 120, 
     position: ['30%',], 
     overlayId: 'msgbox-overlay', 
     containerId: 'msgbox-container', 
     onShow: function(dialog) { 
     var modal = this; 
     $('.header', dialog.data[0]).append('Mailing List'); 
     $('.message', dialog.data[0]).append(msg); 
     $('.ok', dialog.data[0]).click(function() { 
      modal.close(); 
     }); 
     } 
    }); 

    }); 
}); 

Модальное сообщение не заполнено? Ты знаешь почему?

Спасибо!

+0

возможно дубликат [Как вернуть ответ от вызова AJAX?] (Http://stackoverflow.com/questions/14220321/how-to-return- a-response-from-a-ajax-call) –

ответ

1

$.ajax - это асинхронная функция, которая может быть выполнена в любое время. Таким образом, значение переменной msg изменится через некоторое время, до этого будет выполнен код ()$('#simplemodal').modal({, поэтому значение отсутствует. Так просто инициализировать ()$('#simplemodal').modal({ после Ajax успеха или ошибки

jQuery(function($) { 
    $('#mailing').submit(function(e) { 
     e.preventDefault(); 
     var msg = ''; 
     $.ajax({ 
      type: "POST", 
      url: "ajx_mailing.asp", 
      data: '[email protected]', 
      cache: false, 
      dataType: "text", 
      complete: function(data) { 
       if (data.responseText = 'ok') { 
        msg = 'The e-mail was included with success!'; 
       } else { 
        msg = 'There is a problem, please check the e-mail!'; 
       } 
       modal(); 
      }, 
      error: function(data) { 
       msg = 'Error! Please, try again latter!'; 
       modal(); 
      }, 
     }); 
     /* http://www.ericmmartin.com/projects/simplemodal/ */ 

     function modal() { 
      $('#simplemodal').modal({ 
       minWidth: 410, 
       minHeight: 120, 
       position: ['30%', ], 
       overlayId: 'msgbox-overlay', 
       containerId: 'msgbox-container', 
       onShow: function(dialog) { 
        var modal = this; 
        $('.header', dialog.data[0]).append('Mailing List'); 
        $('.message', dialog.data[0]).append(msg); 
        $('.ok', dialog.data[0]).click(function() { 
         modal.close(); 
        }); 
       } 
      }); 
     } 
    }); 
}); 
+0

Отлично! Спасибо, Пранав! – Guybrush

+0

@Paruba: рад помочь –

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