不能得到的.js在表单POST被称为 [英] Cannot get .js to be called upon form POST
问题描述
我有一点code,它的工作,但没有做什么,我想要它做的。
I have a bit of code that's "working," but is not doing what I want it to do.
在我的网页,我在头位的code:
On my page, I have a bit of code in the header:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="update.js"></script>
和在我的主页,我有:
<form id="updateChanges" method="POST" action="update.php">
...code...
</form>
当我点击这个表单里面的按钮,我想update.js执行里面的code,但页面重定向到update.php并执行我的code,而不是成功。我试图绕过这个重定向,并没有做刷新页面执行code。我不明白为什么它不工作。
When I click the button inside of this form, I want the code inside of update.js to execute, but the page redirects to update.php and executes my code successfully instead. I am trying to bypass this redirect, and execute the code without doing a page refresh. I do not understand why it's not working.
我接到了一个教程如何使用AJAX的.js文件。
I got the .js from a tutorial on how to use AJAX.
$(function() {
// Get the form.
var form = $('#updateChanges');
// Set up an event listener for the contact form.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
});
});
我的JavaScript文件不会被调用。为什么?
my javascript file is not being called. Why?
推荐答案
您已经定义的jQuery表单对象中的表格
变量,你不需要把它包jQuery的了。
You've already defined jQuery form object in the form
variable, you do not need to wrap it for jQuery again.
$的所有实例(表)
成为:表格
$(function() {
// Get the form.
var form = $('#updateChanges');
// Set up an event listener for the contact form.
form.submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
// Serialize the form data.
var formData = form.serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: form.attr('action'),
data: formData
});
});
});
这篇关于不能得到的.js在表单POST被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!