weixin_33701294 2015-07-15 11:49 采纳率: 0%
浏览 24

表单上的AJAX参考错误

I have the following AJAX script which is for a contact form on my page

$(function() {

    // Get the form.
    var contactForm = $('#contactForm');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        })
        :
        :

HTML

 <form id="contactForm" name="contactForm" action="php-scripts/contact-form.php" method="post">
           <label for="name">Name</label>
           <br />
           <input type="text" name="name" id="conName" />
           <br />
           <label for="email">Email</label>
           <br />
           <input type="text" name="email" id="conEmail" />
           <br />
           <label for="phone">Phone</label>

My Problem

On my web console I get the error:

ReferenceError: form is not defined

$(form).submit(function(e)

As a newbie to AJAX I cant see why I am getting this error, any help will be appreciated

If you need any more info to help please let me know

  • 写回答

1条回答 默认 最新

  • weixin_33719619 2015-07-15 11:51
    关注

    There is no variables called form defined. Most probably missing the quotes around form -

    $('form').submit(function(e)
    

    OR contactForm is defined

    contactForm.submit(function(e)
    
    评论

报告相同问题?