Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery doesn't submit a form

I have the following HTML code:

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>IT-Services Umfrage - Fragen</title>

    <link rel="stylesheet" type="text/css" href="/IT/umfrage/../../style.css" />
    <link rel="stylesheet" type="text/css" href="/IT/umfrage/css/umfrage.css" />

    <script type="text/javascript" src="/IT/umfrage/../../inc/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="/IT/umfrage/js/umfrage.js"></script>
</head>

<body id="page-show">
    <div class="ueberschriftsbalken"><span>IT-Services Umfrage - Fragen</span></div>
    <br />
    <div id="content">

        <form action="/IT/umfrage/Umfrage/save" method="post" id="umfrageForm">
            <div class="frage radio">
                <p>1. Wie professionell empfinden Sie das Auftreten der IT-Service Anlaufstelle?</p>
                <label for="frage1_1"><input type="radio" name="frage1" id="frage1_1" value="1" /> Lausig</label><br />
                <label for="frage1_2"><input type="radio" name="frage1" id="frage1_2" value="2" /> Schwach</label><br />
                <label for="frage1_3"><input type="radio" name="frage1" id="frage1_3" value="3" /> Durchschnittlich</label><br />
                <label for="frage1_4"><input type="radio" name="frage1" id="frage1_4" value="4" /> Gut</label><br />
                <label for="frage1_5"><input type="radio" name="frage1" id="frage1_5" value="5" /> Hervorragend</label>
            </div>
            <input type="submit" value="Antworten absenden" name="submit" id="submitbutton" />
        </form>
    </div>
</body>
</html>

When clicking on the submitbutton or when pushing the enter key, the form gets submitted. But when i try $('#umfrageForm').submit(), $('form').submit() or anything similar, nothing happens. In Firebug, the function only returns the DOM path to the form element.

What could be the problem that prevents the form from being submitted?

like image 330
Danilo Bargen Avatar asked Jun 25 '10 11:06

Danilo Bargen


People also ask

Why form submit is not working in jQuery?

The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.

How to submit a form in jQuery?

Forms can be submitted either by clicking on the submit button or by pressing the enter button on the keyboard when that certain form elements have focus. When the submit event occurs, the submit() method attaches a function with it to run. It triggers the submit event for selected elements.

Why My submit button not working?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

How do I stop form submit in jQuery?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting.


2 Answers

You have a field named submit, this clobbers the submit method of the form (with a reference to the DOM node for the field) that jQuery depends on in its own submit method.

Rename it.

like image 192
Quentin Avatar answered Oct 25 '22 05:10

Quentin


I think you fire the submit event before the DOM is actually loaded, because if I try it, this code works:

$(document).ready(function(){

    // Submit handler, to prove everything works fine
    $('#umfrageForm').submit(function(){
        alert('hi');
        return false;
    });

    // Fire the submit event
    $('#umfrageForm').submit(); // alerts 'hi'
});

See: jsFiddle

like image 41
Harmen Avatar answered Oct 25 '22 03:10

Harmen