gorn
gorn

Reputation: 8177

JQuery and HTML Form submit

A standard form is submitted and I want jquery to determine if its valid first, returning true or false (need false to work). But it just goes on to the action page in any case.

HTML:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
<link href="user.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript" language="javascript" src="regValidation.js"></script>
</head>
<body>
<form id="regform" action="BookstorePage.php" method="post">            
<label for="f_name">First name</label>
<input type="text" name="f_name" id="REG_FNAME" />                  
<label for="l_name">Last name</label>
<input type="text" name="l_name" id="REG_LNAME" />
<input id="btnsubmit" type="submit" name="reg_submit" value="Register!" />
</form>
</body>
</html>

Jquery script:

$("#regform").submit(function() {
alert('Handler for .submit() called.');
return false;
});

However; $(window).load(function () {}); does work.

Upvotes: 0

Views: 3160

Answers (2)

MilkyWayJoe
MilkyWayJoe

Reputation: 9092

This works for me:

<html>
    <head>
        <title>Test asdfg</title>
        <style type="text/css">
            #iframeStateChangelog {
                height: 80px;
                overflow: auto;
            }
        </style>
    </head>
    <body>
        <form id="regform" action="BookstorePage.php" method="post">            
            <label for="f_name">First name</label>
            <input type="text" name="f_name" id="REG_FNAME" />                  
            <label for="l_name">Last name</label>
            <input type="text" name="l_name" id="REG_LNAME" />
            <input id="btnsubmit" type="submit" name="reg_submit" value="Register!" />
        </form>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
        <script type="text/javascript">

            var RegForm = null;

            var RegForm_OnSubmit = function(e) {
                alert('handler for submit event');
                e.preventDefault();
                return false;
            };

            $(function() {
                RegForm = $("form");
                RegForm.bind('submit', RegForm_OnSubmit);
            });


        </script>
    </body>
</html>

Upvotes: 0

David Rodrigues
David Rodrigues

Reputation: 12562

You need use $(function() { ... }), it will triggered on document ready. Something like:

$(function() {
    $("#regform").submit(function() {
        alert('Handler for .submit() called.');
        return false;
    });
});

Upvotes: 3

Related Questions