开发者

How can I force a form to submit using JavaScript when a person hits the space bar in the input field?

Is that possible? Google searches are leading me nowhere.

My Sample form:

<form action="search.asp" method="post" name="form1">

    User ID <input type="text" size="15" name="userid"><p>
    Last Name <input type="text" size="15" name="lastname"><p>
    School <input type="text" size="15" name="school"><p>
    District <input type="text" size="15" name="district"><p>
    Email <input type="text" size="20" name="email"><p>

    <input type="submit" value=" Go Search! ">

</form>

This needs to work from an开发者_Go百科y input box on the form. I tried onkeyUP but wouldn't work or I probably wrote it wrong. I am no javascript expert. Any ideas?


I don't know why you'd do this, but in Firefox, you would write:

<form action="search.asp" method="post" name="form1" onkeydown="if(event.keyCode == 32) this.submit(); return false;">

Check here to see how to retrieve other browsers' key codes.

Again, this is how you would do it, but I think it's a bad idea.


The whole form to submit whenever a space is detected by may be a little messy, but what's interesting is to do it for a specific input. I used it in a field where user gives some tags. Each tag must de only one word,and so the space character can be used as an event to store the given tag and wait for the next.

<input type="text" id="tagtextbox" onKeyUp="if(event.keyCode == 32) myfunction();" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜