2

I've checked here and here but can't get my form to submit. (noob to Jquery...)

<html>
<head>
    <title>Book Notes Application - Subjects</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
    <script>
        $(document).ready(function(){
            $('#subject_id').change(function(){
                var subject_id = $(this).val();
                //alert(subject_id)
                //document.forms["choose_subject"].submit();
                $('#choose_subject').submit();
                //$('form#choose_subject').submit();
                //$('form#choose_subject')[0].submit();
                //alert(subject_id);
                //document.getElementById("choose_subject").submit();
            })
        })
    </script>
</head>
<body>
    <!-- CHOOSE SUBJECT -->
    <FORM action="/books" id="choose_subject" name="choose_subject" method="POST">
        Choose a Subject:
        <select name="subject_id" id="subject_id">
                <option value="1">a</option>
                <option value="2">a2</option>
        </select><input type="submit" name="submit" value="Choose Subject"/>
        <BR />
    </FORM>
    <!-- CREATE SUBJECT -->
    <FORM action="/subjects" id="create_subject" method="POST">

        <BR /><BR />
        Create a new Subject
        <input type="text" name="subject_name" />
        <input type="submit" name="submit" value="Create Subject" />
    </FORM>
<body>
</html>

I've tried each of these methods:

1) document.forms["choose_subject"].submit();
2) $('#choose_subject').submit();
3) $('#choose_subject')[0].submit();
4) $('form#choose_subject').submit();
5) $('form#choose_subject')[0].submit();
6) document.getElementById("choose_subject").submit();

The first and last result in a "TypeError: document.forms.choose_subject.submit is not a function.

The second and fourth don't have an error but they also doesn't do anything.

The third and fifth error out with a "TypeError: $(...)[0].submit is not a function"

If I remove the second form these results hold true.

Clicking the submit button works as expected.

Community
  • 1
  • 1
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231

1 Answers1

2

Change your the name of your input element:

<input type="submit" name="submit" value="Choose Subject"/>

to something other than "submit". As the .submit() docs state:

Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures.

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272