如何使用ajax提交表单

时间:2015-11-17 13:34:35

标签: javascript php jquery ajax forms

我有我的表格和我的ajax布局,但我不知道如何使用ajax提交表单。我已经尝试了$('#testform').submit()但是当我用提交包裹它时它没有调用我的ajax。我可能做错了。如何通过ajax提交表单而不定期提交?

<form id="testform" action="https://example.com/api/payments/" method="post">

Name<input type="text" name="name" id="name">
Card Number <input type="text" name="card_number" id="card_number" maxlength="16">
Exp Month <input type="text" name="exp_month" id="exp_month">
Exp Year <input type="text" name="exp_year" id="exp_year">
CVC <input type="text" name="cvc" id="cvc" maxlength="3">
Amount  <input type="text" name="amount" id="amount">

<input type="submit" id="submit">

frm = $('#testform');
frm.submit(function(ev)
{
$.ajax({
    type: frm.attr('method'),
    url: frm.attr('action'),
    dataType: "html",
    //Set the HTTP headers for authentication
    beforeSend: function (xhr) {
        xhr.setRequestHeader('api_key', 'tiyndhinzrkzti5ody0');
        xhr.setRequestHeader('email', 'example@example.com');
    },
    //Serialize the data sent from the form inputs
    data: frm.serialize(),
    success: function(data) {
        $('#return').append(data);
   }
});
ev.preventDefault();       
});

1 个答案:

答案 0 :(得分:-1)

而不是program pr2_ex2 implicit none integer::x double precision::dy,dx !dy=the height of the linear part & dx=the lenght of the linear part double precision::x1,x2,s !f=f(x) the function,x=the values which can be given to f double precision::length print*,"Please enter the function's starting point" read*,x1 print*,"Please enter the function's ending point" read*,x2 length = 0 s = 0 do x = 2, 21 dx = ((x*abs(x2-x1)-(x-1)*abs(x2-x1))/(20)) dy = (3.1*(x*abs(x2-x1)/20)**2-(5.3*20/x*abs(x2-x1)))-(3.1*((x-1)*abs(x2-x1)/20)**2-(5.3*20/(x-1)*abs(x2-x1))) length = sqrt((dx**2)+(dy**2)) s = length+s end do print*,s end program 尝试以下代码

frm.submit(function(ev)

单击提交按钮后,您会看到在控制台中发布了ajax。 &#34;魔术&#34;是将一个处理程序附加到submit事件而不是调用事件本身。此外,您之前的代码中有一个拼写错误($("#testform").on('submit', function(ev) { ev.preventDefault(); ... }); 而不是$('testform')