$(form).ajaxSubmit is not a function

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I'm trying to use the jquery validate plugin to validate a form and submit the contents with an ajax request.

This code is in the head of my document.

 $(document).ready(function() {     $('#contact-form').validate({submitHandler: function(form) {          $(form).ajaxSubmit();             contactSuccess() ;          }     }); }); 

The validation works. However, the submission is made normally: On submission, the page reloads. Of course, I've got a non-js fallback behaviour for browsers that don't have js enabled. But I'd like to get the smoother user experience working.

The error that I see in firebug is: $(form).ajaxSubmit is not a function

What could I be doing wrong here?

回答1:

I'm guessing you don't have a jquery form plugin included. ajaxSubmit isn't a core jquery function, I believe.

Something like this : http://jquery.malsup.com/form/



回答2:

this is new function so you have to add other lib file after jQuery lib

it will work.. I have tested.. hope it will work for you..



回答3:

Try:

$(document).ready(function() {     $('#contact-form').validate({submitHandler: function(form) {          var data = $('#contact-form').serialize();             $.post(               'url_request',                {data: data},                function(response){                   console.log(response);                }           );          }     }); }); 


回答4:

Try ajaxsubmit library. It does ajax submition as well as validation via ajax.

Also configuration is very flexible to support any kind of UI.

Live demo available with js, css and html examples.



回答5:

Ajax Submit form with out page refresh by using jquery ajax method first include library jquery.js and jquery-form.js then create form in html:

name: password: Email:


回答6:

Drupal 8

Drupal 8 does not include JS-libraries to pages automaticly. So most probably if you meet this error you need to attach 'core/jquery.form' library to your page (or form). Add something like this to your render array:

$form['#attached']['library'][] = 'core/jquery.form'; 


回答7:

I think you want to do $(this).ajaxSubmit(); ... also, take note of what the example from the function documentation says:

// attach handler to form's submit event $('#myFormId').submit(function() {     // submit the form     $(this).ajaxSubmit();     // return false to prevent normal browser submit and page navigation     return false; }); 

-- i.e. it says that you need to return false; to prevent normal browser submit.



回答8:

$(form).ajaxSubmit();  

triggers another validation resulting to a recursion. try changing it to

form.ajaxSubmit();  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!