jQuery表单.submit();不在Firefox中工作

时间:2017-02-24 21:52:29

标签: javascript jquery html forms

我已经看过一些帖子已经尝试在firefox中提交带有jquery的表单,但我无法在我的表单中使用任何这些解决方案。

我的页面上有两个表单,我想要做的是同时提交它们。我目前有第一个按钮提交表单#2,当提交表单#2时,然后提交表单#1。我必须这样,因为我托管这些表单的地方需要页面重定向。如果两个表单都重定向,我就无法提交两个表单,所以我提交表单#2,阻止重定向,然后提交表单#1。

这两种表单都在Chrome和IE中完美地提交和发布数据,但在使用Firefox时,我无法获得标记为“form2”的表单来发布数据。为什么form1发布数据但是form2在我使用firefox时没有发布数据?

这是我的jsfiddle

/*  trigger second form submit when clicking first form submit */
// form 1 button submits form 2
$('#submitform1').click(function() {
  $('#form2').submit();
});

//on form 2 submit, stop the confirmation page and don't run a function on completion
$('#form2').submit(function(e) {
  e.preventDefault();
  $.ajax({
    type: 'post',
    url: 'url',
    data: $('#form2').serialize(),
    complete: function() {}
  });
});

//when form2 submits, submit form 1
$('#form2').submit(function() {
  $('#form1').submit();
});

1 个答案:

答案 0 :(得分:0)

试试这个,我们将使用promises:

/* create a function to submit the forms */
function formSubmit(el, url) {
    $.ajax({
      type: 'post',
      url: url,
      data: el.serialize(),
      complete: function() {}
    });
}

/* now we say, when first form has been submitted, submit the next */
$.when(formSubmit($('#form2'), 'http://myAjaxUrl2/')).then(function() {
  formSubmit($('#form1'), 'http://myAjaxUrl1/')
});