2016-08-17 2 views
0

Ниже. иметь вызов AJAX, который втягивает .php-файл, чтобы проверять людей ip и показывать их форму, при этом форма должна активироваться. Второй вызов AJAX для отправки данных. Тем не менее, кажется, что вторая функция не активируется при нажатии кнопки передачи, предпочтительно хотите вызвать функцию formSubmit на успех первого вызова AJAX и OnClick активировать эту функцию:функция отправки не работает?

module.exports = { 
    init: function() { 
    $.ajax({ 
    url: "/microsub.php", 
    method: 'GET', 
    success: function (data) { 
     if (data == 1) { 
     $('#rdm-below-header').append('<div id=\"modal\" class=\"modalStyle\">' + 
       '<div>' + 
       '<button type=\"button\" id=\"close\" class=\"close\" data-dismiss=\"modal\" aria-label=\"close\"><span aria-hidden=\"true\">&times;</span></button><br>' + 
       '<div id=\"titleText\" style=\" text-align:center; font-size: 24px; margin-top: 15px;\">Fill in your details for 24hr access to Risk.net</div><br>' + 
       '<form style="text-align:center; clear:both" id="microsubs_form">' + 
       '<input type=\"text\" id=\"ms_firstName\" name=\"ms_firstName\" required placeholder=\"First Name\" style=\"float:left;\" >' + 
       '<input type=\"text\" id=\"ms_lastName\" name=\"ms_lastName\" required style=\"float:left; margin-left:20px;\" placeholder=\"Last Name\">' + 
       '<input type=\"email\" id=\"ms_email\" name=\"ms_email\" required placeholder=\"Corporate Email address\" pattern=\"^.*(\*barclays|\*barcap.com).*$\" oninvalid=\"this.setCustomValidity(\'Please enter your corporate email\')\" style=\"float:left; margin-top: 10px;\">' + 
       '<input type=\"password\" id=\"ms_password\" name=\"ms_password\" required style=\"clear:right; margin-top: 10px; margin-left: 20px;\" placeholder=\"Password\" pattern=\".{6,}\">' + 
       '<input type=\"text\" id=\"microsub_flag\" name=\"microsub_flag\" hidden=\"true\">' + 
       '<input type=\"submit\" name=\"submit\" style=\"alignment-adjust:central; margin-top:30px; clear:right;\" class=\"msProcess\" id=\"submit\" onclick=\"formSubmit()\"><br>' + 
       '</form>' + 
       '<div style=\"text-align:center; clear: both; font-size: 16px; margin-top: 5px; \"><br>' + 
       'If you already have a subscription, <a href=\"login\">sign in here.</a>' + 
       '</div>' + 
       '</div>' + 
       '</div>'); 
     } 
     console.log(data); 
    }, 
    error: function (error) { 
     console.log(error); 
    } 
    }); 

    //Display IP Form 

    //On Submit form via AJAX 

    // Bind to the submit event of our form 
    function formSubmit() { 
    $("#microsubs_form").submit(function (event) { 
     var request; 
     // Abort any pending request 
     if (request) { 
      request.abort(); 
     } 
     // setup some local variables 
     var $form = $(this); 
     // Let's select and cache all the fields 
     var $inputs = $form.find("input, select, button, textarea"); 
     // Serialize the data in the form 
     var serializedData = $form.serialize(); 
     // Let's disable the inputs for the duration of the Ajax request. 
     // Note: we disable elements AFTER the form data has been serialized. 
     // Disabled form elements will not be serialized. 
     $inputs.prop("disabled", true); 
     // Fire off the request to /form.php 
     request = $.ajax({ 
     url: "/ms_form_handler.php", 
     type: "POST", 
     data: serializedData, 
     success: function (data) { 
      console.log(data); 
     }, 
     error: function (error) { 
      console.log(error); 
     } 
     }); 
     // Prevent default posting of form 
     event.preventDefault(); 
    }); 
    } 
    ; 
}}; 
//On Success Hide form, show success message ask to login 
//Set cookie to hide the form 
//utility.setCookie(name); 
+0

Возможно, ваш '$ ('# micro_subs_form')' выполняется, когда этот идентификатор dom еще не существует, поэтому для отправки заявки на отправку ничего не существует. –

+0

Marc B, даже если после создания события и я передаю через .on() i.e ("msProcess") on ('click', 'submit', function (event) {}) – Yondaime14

ответ

1

Вам нужно связать событие, когда первый аякс завершен. Попробуйте добавить

formSubmit(); 

в функцию успеха в первом вызове ajax.

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