2014-01-14 4 views
0

This is the link that i'm working withОтправка формы Динамическая

Я стараюсь избегать использования form.submit для того, чтобы купить товар. Пользователь нажимает на элемент, затем должен подтвердить покупку. Это действительно не форма, которая отправляется, но вместо этого она выполняет функцию shown here. Вы можете использовать копию и вставить строку и использовать элемент управления + F, чтобы перейти к той части скрипта, где удерживается функция: (;// pages/PurchaseConfirmationModal.js)

Я искал метод POST, и я не смог понять, как заставить это работать;

$.post("Form here", // How to identify the form? 
    function(data) { 
     // how do I send the data? 
    } 
); 

ответ

1

Отдайте поля ввода в вашей форме ID: s, то вы можете получить их значение для того, чтобы передать его на $ .post, например,

<input type="text" id="input_one"/> 
<input type="text" id="input_two"/> 

И потом:

var post_data={ 
    first_value: $("#input_one").val(), 
    second_value: $("#input_two").val() 
}; 

$.post("http://...",post_data, 
    function(data) { 
     // Handle the response from the server here. 
    } 
); 
0

HTML ЧАСТЬ

<form> 
    <input type="text" value="" name="first_value" id="first_value" /> 
    <input type="text" value="" name="second_value" id="second_value" /> 

    <button id="submitForm" >Submit</button> 
</form> 

AJAX Часть Вызов функции, нажав на кнопку. Затем собирайте данные формы, используя $("input[id=first_value]").val() или $("#first_value").val() До вас.

$('#submitForm').click(function(event){  
    // get the form data 
    // there are many ways to get this data using jQuery (you can use the class or id also) 
    var formData = {  
      'first'    : $("input[id=first_value]").val(), 
      'second'   : $("input[id=first_value]").val(), 
    }; 


    // process the form 
    var ajaxResponse = $.ajax({ 
           type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
           url   : 'someURL', // the url where we want to POST 
           data  : JSON.stringify(formData), 
           contentType :'application/json', 
           error  : function(data,status,error){ 
               console.log(data+': '+status+': '+error); 
               }, 
           success  : function(status) { 
                //DO something when the function is successful 
               }  
        }).done(function(apiResponse) { 
          //Do something when you are done 
        }); 

}); 
Смежные вопросы