【问题标题】:jQuery form .submit(); not working in FirefoxjQuery 表单 .submit();不能在 Firefox 中工作
【发布时间】:2017-07-15 21:26:06
【问题描述】:

我已经看到一些帖子已经尝试在 Firefox 中使用 jquery 提交表单,但我无法让任何这些解决方案在我的表单中工作。

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

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

这是我的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();
});

【问题讨论】:

  • form2提交完成后提交form1?是这个意思吗?
  • 是的,当 form2 提交时,form1 应该提交。我尝试在 ajax Complete 中完成此操作,但它不起作用,所以我不得不将它从那里拉出来并将它放在底部,如您所见
  • 它应该在 ajax 成功,就这么简单——如果这不起作用,你需要显示你的代码,因为这是正确的方法——除非你想使用 Promise,但是我会先试试这个
  • 我已经尝试过了,但我无法提交任何一个表单:success:function(){ $('#form1').submit(); }
  • 任何一种形式?您在浏览器控制台中看到了什么错误?

标签: javascript jquery html forms


【解决方案1】:

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

/* 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/')
});

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多