不能得到的.js在表单POST被称为 [英] Cannot get .js to be called upon form POST

查看:119
本文介绍了不能得到的.js在表单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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆