简体   繁体   English

如何提交已被event.preventDefault()拦截的表单

[英]How to submit a form that has been intercepted with event.preventDefault()

I have a plain vanilla form with id="edit_profile". 我有一个带有id =“ edit_profile”的普通香草表单。

When the form is submitted I want to intercept it and check whether I need to geocode an address via the google maps api, and if not go ahead and submit anyway. 提交表单后,我想拦截它,并检查是否需要通过google maps api对地址进行地址解析,如果不需要,则继续提交。

Something like: 就像是:

$('#edit_profile').on('submit', function(event) {
    event.preventDefault();
    if ( some test ) {
        geocode_address( address, this ); // the api callback will handle form submit
    } else {
    // we want to go ahead and submit the form
    this.submit();
    }
});

The problem is this.submit() is throwing the error TypeError: Property 'submit' of object #<HTMLFormElement> is not a function 问题是this.submit() TypeError: Property 'submit' of object #<HTMLFormElement> is not a function错误TypeError: Property 'submit' of object #<HTMLFormElement> is not a function

I read something recently but can't now find about having to use native JavaScript to submit the form rather than jQuery (I'm guessing because the jQuery event handler for the form submit has already been told to preventDefault). 我最近读了一些书,但现在找不到必须使用本机JavaScript来提交表单而不是jQuery的信息(我猜是因为已经告知表单提交的jQuery事件处理程序可以防止preventDefault)。

But if I try some variant like document.getElementById('edit_profile').submit(); 但是如果我尝试一些类似document.getElementById('edit_profile').submit();变体document.getElementById('edit_profile').submit(); or document.forms[0].submit(); document.forms[0].submit(); I get the same error or thereabouts. 我得到相同的错误或左右。

DOM Elements have no method .submit -- but jQuery objects do (only on form elements) -- basically, you forgot to wrap this in jQuery: DOM元素没有方法.submit -但jQuery的对象做(仅限于form元素) -基本上,你忘了换this jQuery中:

$(this).submit();

Ref: https://api.jquery.com/submit/ 参考: https : //api.jquery.com/submit/

Edit: 编辑:

I see where you are getting the "infinite-loop" scenario from. 我知道您从哪里得到“无限循环”方案。 You should actually be setting the handler up to trigger when you click the Submit button and not the forms actual submit. 实际上,您应该将处理程序设置为在您单击“提交” button而不是表单实际提交时触发。

As of now, you catch the forms submit action and prevent the default action (which is submitting) -- then you do some stuff and call submit on this . 到目前为止,您捕获了表单submit操作,并阻止了默认操作(正在提交)-然后您做了一些事情,并在this上调用submit Well that will trigger the handler for the form submit , which prevents the default action again! 好吧,这将触发表单submit的处理程序,这将再次阻止默认操作! So, in short, put a handler on your Submit button, prevent the default action, do logic, then call $("#edit_profile").submit() 因此,简而言之,在您的Submit按钮上放置一个处理程序,阻止默认操作,执行逻辑,然后调用$("#edit_profile").submit()

Why not rearrange the code something like this 为什么不重新排列这样的代码

$('#edit_profile').on('submit', function(event) {
  if ( some test ) {
    event.preventDefault();
    geocode_address( address, this ); // the api callback will handle form submit
  } else {
    // we want to go ahead and submit the form
    // this.submit(); // no need to call explicitly
  }
});

It won't work, if you want to have callback in some test but it doesn't look like that from your code. 如果您想在some test具有回调功能,则它将无法正常工作,但从您的代码中看起来却并非如此。

I'm answering my own question because the solution was so unexpected and simple it bears repeating. 我正在回答自己的问题,因为解决方案是如此出乎意料且简单,值得重复。 Full credit to Chris Butler for answering this related question: Unable to `submit()` an html form after intercepting the submit with javascript (note, his wasn't the accepted answer). 克里斯·巴特勒(Chris Butler)回答了这个相关问题,这一切都得到了满分: 在用javascript拦截提交后,无法“提交()”一个html表单 (请注意,他不是被接受的答案)。

Answer: if you name & #id your submit buttons "submit" that will clash with the name of the native JavaScript .submit() method which will no longer work and give the type error described in the question. 答:如果您命名&#id,则提交按钮“提交”将与本机JavaScript .submit()方法的名称发生冲突,该方法将不再起作用并给出问题中所述的类型错误。

So the solution in my case was to rename my submit button to something else. 所以我的解决方案是将我的提交按钮重命名为其他名称。

To recap: 回顾一下:

To intercept a form submission you could intercept the click on the submit button or intercept the form submission itself. 要拦截表单提交,您可以拦截单击“提交”按钮或拦截表单提交本身。 If your objective is to intercept the form submission then do that, not the click event on the submit button, as forms can be submitted by other events such as hitting enter on a textfield. 如果您的目标是拦截表单提交,则可以执行此操作,而不是单击“提交”按钮上的click事件,因为表单可以通过其他事件提交,例如在文本字段上按回车键。

If you intercept the form via jQuery with, eg $("#myForm").on("submit", function() {...}) and then go on to pass off the submit handling to another function (eg as in the question where a call to a geocoding api is made) then that function will need to trigger the form submission via acting on the DOM form element directly (eg `document.getElementById('myForm').submit() ) which will submit the form without triggering the onsubmit event so that the initial intercept isn't triggered again creating an infinite loop as it would be if you used jQuery to trigger the submit. 如果您使用$("#myForm").on("submit", function() {...})通过jQuery截取表单,然后继续将提交处理传递给另一个函数(例如问题(在哪里调用地理编码api),则函数将需要通过直接作用于DOM表单元素(例如`document.getElementById('myForm')。submit())来触发表单提交。表单而不会触发onsubmit事件,这样就不会再次触发初始拦截,从而不会产生无限循环,就像使用jQuery触发提交一样。

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

相关问题 如何在event.preventDefault之后提交表单 - How to submit a form after event.preventDefault 如何在event.preventDefault();之后提交表单? - How to submit form after event.preventDefault();? 在 event.preventDefault 之后提交表单(event.preventDefault 行为) - Submit a form after event.preventDefault (event.preventDefault behavior) 如何重置表单提交事件的 event.preventDefault()? - How to reset event.preventDefault() of a form submit event? 在event.preventDefault()之后如何在ajax .done()中提交表单? - How to submit a form in ajax .done() after event.preventDefault()? event.preventDefault(); 在表单提交时没有在firefox上工作 - event.preventDefault(); is not working on firefox on form submit 当我的网址有? 标记event.preventDefault()或返回false不适用于表单提交事件 - when my url has ? mark event.preventDefault() or return false don't work on form submit event 不使用event.preventDefault阻止html5表单提交 - Prevent html5 form submit without using event.preventDefault React 提交表单返回 event.preventDefault 不是 function - React submit form returns event.preventDefault is not a function event.preventDefault(); 仅在某些时候可以使用提交表单 - event.preventDefault(); only works some of the time with submit form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM