2

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">    
1
  • I added an update to my answer explaining why your new code snippet doesn't work. Commented Feb 16, 2012 at 20:29

1 Answer 1

3

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').

Sign up to request clarification or add additional context in comments.

2 Comments

This was not the problem, because I made a mistake while posting the code. I don't understand why this is not working ...
I had two different .html files for each page. Since a transfered the code from the second file to the first I dont't have the problem anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.