jQuery $(form).ajaxSubmit 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5265914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(form).ajaxSubmit is not a function
提问by Bob Dobbs
I'm trying to use the jquery validate plugin to validate a form and submit the contents with an ajax request.
我正在尝试使用 jquery 验证插件来验证表单并使用 ajax 请求提交内容。
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.
验证有效。但是,提交是正常进行的:提交时,页面会重新加载。当然,对于没有启用 js 的浏览器,我有一个非 js 回退行为。但我希望获得更流畅的用户体验。
The error that I see in firebug is: $(form).ajaxSubmit is not a function
我在萤火虫中看到的错误是:$(form).ajaxSubmit is not a function
What could I be doing wrong here?
我在这里做错了什么?
回答by Mike Park
I'm guessing you don't have a jquery form plugin included. ajaxSubmit
isn't a core jquery function, I believe.
我猜你没有包含 jquery 表单插件。 ajaxSubmit
我相信这不是核心 jquery 函数。
Something like this : http://jquery.malsup.com/form/
像这样的东西:http: //jquery.malsup.com/form/
UPD
UPD
<script src="http://malsup.github.com/jquery.form.js"></script>
回答by Sandeep Kapil
this is new function so you have to add other lib file after jQuery lib
这是新功能,因此您必须在 jQuery lib 之后添加其他 lib 文件
<script src="http://malsup.github.com/jquery.form.js"></script>
it will work.. I have tested.. hope it will work for you..
它会起作用..我已经测试过..希望它对你有用..
回答by Vishnu Sharma
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:
Ajax 使用 jquery ajax 方法提交不带页面刷新的表单,首先包含库 jquery.js 和 jquery-form.js 然后在 html 中创建表单:
<form action="postpage.php" method="POST" id="postForm" >
<div id="flash_success"></div>
name:
<input type="text" name="name" />
password:
<input type="password" name="pass" />
Email:
<input type="text" name="email" />
<input type="submit" name="btn" value="Submit" />
</form>
<script>
var options = {
target: '#flash_success', // your response show in this ID
beforeSubmit: callValidationFunction,
success: YourResponseFunction
};
// bind to the form's submit event
jQuery('#postForm').submit(function() {
jQuery(this).ajaxSubmit(options);
return false;
});
});
function callValidationFunction()
{
// validation code for your form HERE
}
function YourResponseFunction(responseText, statusText, xhr, $form)
{
if(responseText=='success')
{
$('#flash_success').html('Your Success Message Here!!!');
$('body,html').animate({scrollTop: 0}, 800);
}else
{
$('#flash_success').html('Error Msg Here');
}
}
</script>
回答by Edgar
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);
}
);
}
});
});
回答by MiSHuTka
Drupal 8
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:
Drupal 8 不会自动将 JS 库包含到页面中。因此,很可能如果您遇到此错误,您需要将“core/jquery.form”库附加到您的页面(或表单)。将这样的内容添加到您的渲染数组中:
$form['#attached']['library'][] = 'core/jquery.form';
回答by Bogdan Gusiev
Try ajaxsubmitlibrary. It does ajax submition as well as validation via ajax.
尝试ajaxsubmit库。它通过ajax进行ajax提交和验证。
Also configuration is very flexible to support any kind of UI.
此外,配置非常灵活,可以支持任何类型的 UI。
Live demo availablewith js, css and html examples.
现场演示可用于 js、css 和 html 示例。
回答by tildemark
$(form).ajaxSubmit();
triggers another validation resulting to a recursion. try changing it to
触发导致递归的另一个验证。尝试将其更改为
form.ajaxSubmit();