user1327904
user1327904

Reputation:

Can't get jQuery form submit to work

For some reason, when the form button is clicked, the jQuery script I wrote isn't running, does anyone know why?

<body>
    <form id="inputform" action="google.com">
        <text id="text">Enter Your Number:</text>
        <input id="input" name="input" type="text">
    <input id="submitArea" type="submit" value="">
    </form>
</body>
$('#inputform').submit(function() {
    window.location = "http://mysite.com/";
});

Yes, I imported the jQuery library and everything, I've sourced the external JS file, but I can't figure out why it still isn't working.

Upvotes: 0

Views: 92

Answers (3)

SajithNair
SajithNair

Reputation: 3867

Ideally you should not do a window.location call from inside a submit button. The data you entered in the Form's text input field wont be automatically posted to the action page if you do so.

<body>
<form id="inputform" action="http://mysite.com/">
    <text id="text">Enter Your Number:</text>
    <input id="input" name="input" type="text">
<input id="submitArea" type="submit" value="">
</form>

Upvotes: 0

karthikr
karthikr

Reputation: 99620

You need to prevent the default action from occuring. You can do that by using preventDefault action on the event e. Something like this:

$(function(){
    $('#inputform').submit(function(e) {
        e.preventDefault();
        window.location = "http://mysite.com/";
    });
});

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Assuming your script is inside document ready, else you need to move the script inside `jQuery(function($){.....});

You need to prevent the default action of the submit button

$('#inputform').submit(function(e) {
    window.location = "http://mysite.com/";
    return false; // or call e.preventDefault();
});

Upvotes: 0

Related Questions