jokober
jokober

Reputation: 864

Submit JQuery mobile form without redirect to php file

I'm new to JavaScript and also the JQuery mobile framework is new to me.

I made a small form and after submitting I want to run JavaScript Code to check if all inputs are given. I found a lot of examples how to redirect to php files but I don't understand how I can run the JS Code and use the given input there ...

I tried to redirect to my JS Code using this line:

<form action="#" id="Form" onsubmit="return check()">

I defined the function check() but nevertheless the error message is that the variable check is not found ... Hope someone can help me ...


I tried Ben Lee's approach but I don't understand why this is not working:

$('#Form').submit(function(ev) {
console.log("nice!!");
});

The button:

<input type="submit" value="Send">    

Upvotes: 2

Views: 1301

Answers (1)

Ben Lee
Ben Lee

Reputation: 53349

Don't use an onsubmit attribute. Instead attach an event (see http://api.jquery.com/submit/):

$(document).ready(function() {
    $('#Form').submit(function(ev) {
        if (!check()) return false;
    });
});

If you do it this way, you don't have to define a separate check function. You can just put the validation logic directly in the handler if you want. Which you decided to do depends on other implementation factors.

UPDATE: $("#form1").submit(...) does not work because "#form1" is not the right selector. When you use a selector of the form "#something" that looks for an element with an id attribute of "something". Since you form has id='Form', you need to use $('#Form').

Upvotes: 3

Related Questions