2

I'm having a problem with onSubmit. I'm building a little mobile website using jQTouch and. I want to call a function with onSubmit, if the user hits Enter, but that does not work. I really don't know what's the problem, because the function works perfectly, when it's called by onclick.

So here's the HTML:

<form action="" method="get" name="form1" onSubmit="calculateValue1();return false">
        <ul>
        <li>
        Input 1 <input id="input1" name="input1" type="number"/> 
        </li>                     
        <li>
        Input 2 <input id="input2" name="input2" type="number"/>
        </li>  
        </ul>
</form>   

<a  onclick="calculateValue1()">Calculate</a>

And here's the javascript for the function calculateValue1():

function calculateValue1() {    
    M = window.form1.input1.value;
    n = window.form1.input2.value;

    if (window.form1.input1.value=="") {
    alert("Value missing");
    }
}

When clicking on the link with onclick, the function works. When the focus is in input field 1 and I hit enter, nothing happens.

What could be the problem with this? I don't get an error message when hitting enter, I really don't know what's the problem with onSubmit.

2 Answers 2

2

You're missing a submit button in your form.

<form onsubmit="alert('here'); return false;" name="form1" method="get">
        <ul>
        <li>
        Input 1  
        </li>                     
        <li>
        Input 2 
        </li>  
        </ul>
<input type="submit" value="submit" style="display:none" />
</form>   
Sign up to request clarification or add additional context in comments.

4 Comments

Um, okay, the submit button is hidden now, but the function is not called, when hit enter :/ when I don't hide the button, it works...
That's weird. Note that my onsubmit is not the same as yours.
Yeah it's strange. But with style="visibility:hidden" the submit button is hidden and the function is called. Thanks!
user, please close this question by marking an answer as final.
0

The onSubmit event is triggered when you submit a form either via the pressing an input of type "submit" or when you press enter in an input field, but in order for the form to submit on pressing the enter key you need to have a proper submit button. You won't need a anchor link for the submit then:

<form action="" method="get" name="form1" onSubmit="calculateValue1();return false">
    <ul>
    <li>
    Input 1 <input id="input1" name="input1" type="number"/> 
    </li>                     
    <li>
    Input 2 <input id="input2" name="input2" type="number"/>
    </li>  
    </ul>
    <input type="submit" value="Calculate"></input>
</form>

Comments

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.