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 on开发者_运维问答click, 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.
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>
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>
精彩评论