在jQuery ajax回调函数中维护函数顺序

时间:2022-12-08 19:43:38

I have a jQuery ajax function. the callback function consists of two functions:

我有一个jQuery ajax函数。回调函数包含两个函数:

1) Take the ajax result (which is an html form) and insert it as an html span's inner html. 2) Submit this form

1)获取ajax结果(这是一个html表单)并将其作为html span的内部html插入。 2)提交此表格

How can I ensure that the form is loaded before JavaScript tries to submit it?

在JavaScript尝试提交表单之前,如何确保加载表单?

Code:

码:

$("select").live("change", function(){
  FormUpdate();
})

function FormUpdate() {

  $.ajax({
  type: "POST",
  url: "index.php?UpdateForm=Yes",
  data: $("#Form").serialize(),
  success: function(msg){
    $("span#Content").html(msg);
    $("#Form").submit();
  }
  });

}

My problem comes because javascript tries to submit the form before it has loaded and my data submitted.

我的问题来了,因为javascript尝试在加载表单和提交数据之前提交表单。

2 个解决方案

#1


1  

Just simply put the function for taking ajax result and insert into the DOM in front and the form submission function at the back.

只需简单地设置获取ajax结果的函数并插入前面的DOM和后面的表单提交功能。

Only after insertion is done, the next function will be called.

只有在插入完成后,才会调用下一个函数。

Example:

例:

$.ajax({
   type: "POST",
   url: "backend.php",
   data: "name=John",
   dataType: "json"
   success: function(msg){
     $("#thediv").html(msg.html);
     $("form#theform").submit();
   }
 });

#2


0  

DOM manipulations are synchronous so the form wont be submitted until afterwards. Are you having a specific problem with this scenario? Please post tons of code, if so :)

DOM操作是同步的,因此表单之前不会提交。您是否遇到此方案的特定问题?请发布大量代码,如果是这样:)

#1


1  

Just simply put the function for taking ajax result and insert into the DOM in front and the form submission function at the back.

只需简单地设置获取ajax结果的函数并插入前面的DOM和后面的表单提交功能。

Only after insertion is done, the next function will be called.

只有在插入完成后,才会调用下一个函数。

Example:

例:

$.ajax({
   type: "POST",
   url: "backend.php",
   data: "name=John",
   dataType: "json"
   success: function(msg){
     $("#thediv").html(msg.html);
     $("form#theform").submit();
   }
 });

#2


0  

DOM manipulations are synchronous so the form wont be submitted until afterwards. Are you having a specific problem with this scenario? Please post tons of code, if so :)

DOM操作是同步的,因此表单之前不会提交。您是否遇到此方案的特定问题?请发布大量代码,如果是这样:)