【问题标题】:Transmit AJAX data after submit提交后传输 AJAX 数据
【发布时间】:2012-10-30 15:54:53
【问题描述】:

我使用下面的代码提交我的表单而不重新加载页面,但是当AJAX成功部分的data.a="a"或"b"等时,我仍然想提交表单为通常由没有 jQuery 的 php 完成。

所以在这种情况下,目的是将 AJAX 数据(“data:myForm.serialize()+”&id_town="+id_town,")传输到表单的“action”html 属性中指定的 url。

你有什么想法吗?

HTML代码:

<form id="inscription_form_home" action="index.php?module=membres&action=inscription" method="post" >
...
</form>     

jQuery 代码:

$("#inscription_form_home").submit(function() { 
var myForm = $(this);

$.ajax({
    dataType: 'json',
    type: "POST",
    url: "traitement_profil.php",
    data:myForm.serialize()+"&id_town="+id_town,
    success: function(data){
        if (data.a == "a" || data.a == "b" ){
            // Transmit the data part to the form url
        }else{                  
            alert("Inscription OK.");
        }
    }
});

return false;
});

【问题讨论】:

  • 你问如何对action url 进行第二次ajax 调用?
  • 解释得很差,看不懂你想要什么
  • @charlietfl :是的,我认为这可能是解决方案。我们该怎么做?
  • 需要清晰简洁的重定向顺序。忘记代码..在问题主体中解释行为开始到结束
  • 好的,我刚刚编辑了我的问题。

标签: ajax jquery


【解决方案1】:

正如已经说过的,您可以在

中进行另一个 ajax 调用

cmets之后的版本,:

试试这个...

<form id="inscription_form_home" action="index.php?module=membres&action=inscription" 

    method="post" >
    ...
    <button id="mButton">Submot<button>
    </form> 


$(document).ready(function() {$("#mButton").bind('click', mysubmit($));});

   function mysubmit()
   {    
    $.ajax({
        dataType: 'json',
        type: "POST",
        url: "traitement_profil.php",
        data:myForm.serialize()+"&id_town="+id_town,
        success: function(data){
            if (data.a == "a" || data.a == "b" ){
                //do your new call here, or you can also submit the form
            }else{                  
                alert("Inscription OK.");
            }
        }
    });    
    return false;
    }

【讨论】:

  • 我试过 $("#inscription_form_home").submit();而不是 if 循环内的注释,而是一次又一次地提交表单。我认为解决方案没有那么简单。
  • 是的,没错,您将 ajax 函数添加到了提交函数中!嘿嘿抱歉没注意到。我建议一种解决方法是:为 ajax 调用创建新函数并将其绑定到发送按钮,并将提交保留在回复中。
  • 我明白,但我不确定能否做到这一点...你能通过编辑你的帖子告诉我代码是什么样的吗?
【解决方案2】:

你可以这样:

$(location).attr('href',YOUR URL);

【讨论】:

  • 我的问题不是简单的重定向:除了重定向之外,我还想传输 AJAX 数据。
  • 像以前一样在您的范围内编写另一个 AJAX 请求。并在重定向之后。但提出一个大的要求总比提出许多小要求要好。
猜你喜欢
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 2017-05-04
  • 2020-06-06
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2018-09-30
相关资源
最近更新 更多