在网站上输入多个表单的键 [英] Enter key with multiple forms on a website

查看:120
本文介绍了在网站上输入多个表单的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站有两种形式。一个用于搜索,另一个用于登录。当我使用回车键提交时,搜索总是被调用,因为它是页面上的第一个表单。



我想要做的是将回车键编程为当某个文本框具有焦点时,请点击某个按钮。



我使用asp:textbox和asp:button作为我的登录表单。



 <$ c 

创建一个全局变量,用于跟踪哪种表单处于焦点状态。 $ C><脚本> var currentForm = document.forms [0];< / script>
< form ...>< input onfocus =currentForm = this.form;/>< / form>
< form ...>< input onfocus =currentForm = this.form;/>< / form>

将一个键盘处理程序添加到窗口中,查找Enter键,然后在提交表单时提交表单这是重点:$ b​​
$ b pre $ 函数globalKeyPressed(event){
if(event.keyCode == ENTER){//这是伪代码,检查如何真正做到这一点
currentForm.submit();
}
}


I have a website with 2 forms. One for search and another for the login. When I use the enter key to submit, the search is always called because it is the first form on the page.

What I want to do is program the enter key to click a certain button when a certain textbox has focus.

I'm using asp:textbox and asp:button for my login form.

解决方案

Create a global variable which tracks what form is in focus:

<script> var currentForm = document.forms[0];</script>
<form ...><input onfocus="currentForm = this.form;"/></form>
<form ...><input onfocus="currentForm = this.form;"/></form>

Attach a keyboard handler to the window, look for the Enter key, then when it's hit submit the form that's being focused:

 function globalKeyPressed(event) {
   if (event.keyCode == ENTER) { // This is pseudo-code, check how to really do it
     currentForm.submit();
   }
 }

这篇关于在网站上输入多个表单的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆