简体   繁体   English

表单提交后如何进行Ajax POST

[英]How to Ajax POST after a Form Submit

I think this is pretty simple but I cannot find anywhere how to do this, as the title says, how do you do an ajax post after a successful submit form post. 我认为这很简单,但是我找不到任何方法,正如标题所述,成功提交表单后如何处理ajax。 I tried to search for it but all I see is the reverse of what I need which is submit after an ajax post. 我尝试搜索它,但所看到的只是我需要的内容,而在ajax发布之后提交该内容是相反的。 I'll try to make a draft of a program very similar to what Im working on. 我将尝试草拟与Im正在研究的程序非常相似的程序。
This is my form. 这是我的表格。

<h2>My Form </h2>
@using (Html.BeginForm(new { @class = "submitForm" }))
{
    <label>Loan Amount</label>
    @Html.DropDownListFor(model => model.Loan.LoanAmount, Model.DropDownOfLoanAmount, new { @class = "LoanAmount", @data_bind = "value: selectedLoanAmount" })
    @Html.ValidationMessageFor(model => model.Loan.LoanAmount)

    <label>Loan Receivable</label>
    @Html.TextBoxFor(model => model.Loan.LoanReceivable, "{0:0,0.00}", new { @class = "LoanReceivable", @readonly = true, dir = "rtl", @data_bind = "value: loanReceivable" })
    @Html.ValidationMessageFor(model => model.Loan.LoanReceivable)

    <label>Interest</label>
    @Html.TextBoxFor(model => model.Loan.Interest, "{0:0,0.00}", new { @readonly = true, @class = "Interest", dir = "rtl", @data_bind = "value: interest" })

    <table class="input-group">
        <tbody data-bind="foreach: loanDeductions">
            <tr>
                <td><strong data-bind='text: deductionName'></strong></td>
                <td>
                    <input class="deductionCode form-control" data-bind='value: amount, valueUpdate: "afterkeydown"' /></td>
                <td><a href='#' data-bind='click: $parent.removeLine'>Delete</a></td>
            </tr>
        </tbody>
    </table>

    <button type="button" class="btn btn-danger">Save Deduction</button>

    <button type="submit" class="btn btn-primary">Save changes</button>
}

This is an example ajax post (dont mind the logic of the post): 这是一个有关ajax帖子的示例(不要介意该帖子的逻辑):

$.ajax({
 'url' :'@Url.Action("UpdateDeductionValues","LoanApp")',
 'data': {amount : $('.LoanAmount').val()},
 'success': function(result) {
 $.each(result, function (idx, items) {
 $('.' + items.Code + '').val(items.Amount.toFixed(2));
  });
  }});

Now what I want to happen is when I submit that form, if the submit is successful, the ajax post is triggered. 现在,我想做的就是提交表单时,如果提交成功,就会触发ajax发布。 So its like posting 2 times in one button, the difference is that the other one is a form submit while the other is an ajax post. 因此,就像在一个按钮中发布两次一样,不同之处在于另一个是表单提交,而另一个是ajax发布。 PLS help. PLS帮助。

What you should do is "take over the submit button" with your jQuery, and then use an ajax call inside. 您应该做的是使用jQuery“接管提交按钮”,然后在内部使用ajax调用。

For example having this submit button: 例如,具有此提交按钮:

<input type="submit" value="Submit" onsubmit="$('#your-form-id-here').submit()">

And having this jQuery submit function with an ajax call, should give you a pretty good idea on what to do. 并通过ajax调用使用此jQuery Submit函数,应该给您一个很好的主意。

$('#your-form-id-here').submit(function (e) {

    e.preventDefault();
    var senddata = $(this).serializeArray();
    var sendto = $(this).attr("action");

    $.ajax({
        url: sendto,
        type: 'POST',
        data: senddata,
        success: function (data) {
            $('.messages').html(data);
        },
        error: function (error) {
            $('.messages').html(error);
        }
    });
});

This basically takes your normal form, and send it through your ajax call, to your normal form action, so basically it works just like normal, but you now have the opportunity to do stuff in your form action php file, and also in your ajax success data. 这基本上采用您的正常形式,并通过您的ajax调用将其发送到您的正常形式动作,因此基本上它的工作原理与正常一样,但是您现在有机会在您的形式动作php文件以及ajax中进行操作成功数据。 For example you could use this to deliver validation messages directly to your user, without refreshing your site. 例如,您可以使用它直接向用户传递验证消息,而无需刷新站点。 And so on... 等等...

i'm not sure what you are trying to do, what the point of doing ajax request after submitting the form the page will reload anyway. 我不确定您要做什么,提交页面后将要重新加载页面的ajax请求的意义是什么。

however if need need do the ajax request after the submitting the form you just need an ajax call. 但是,如果需要在提交表单后执行ajax请求,则只需要一个ajax调用即可。 but that that will work on every time when page load. 但这将在每次页面加载时都起作用。

what i think you need to do make two ajax request the first one is for the submitting the form and if the call is successful then call the second request. 我认为您需要做两个ajax请求,第一个是提交表单,如果调用成功,则调用第二个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM