Form is submitted twice when Enter key is pressed twice

本文讨论了一个在使用JavaScript时遇到的问题:当用户快速连续按下Enter键两次时,如何避免触发按钮点击事件两次。解决方案是在第一次触发点击事件后禁用Enter键响应,确保只触发一次按钮点击。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Thread Tools   Display Modes
  View First Unread
 
Old 04-26-2006, 06:08 AM
  #1
Novice (Level 1)
 
Join Date: Apr 2006
Posts: 1
iTrader: ( 0)
Romil is an unknown quantity at this point
Form is submitted twice when Enter key is pressed twice

I've to capture user's Enter key press, so that an HTML button click can be simulated on event of Enter key press. This is how I do it:

<script language="javascript" type="text/JavaScript"">
var submitBtnObj = document.getElementsByName("saveButton"); // Button whose click has to be simulated on keypress

document.onkeypress = keyPress;

//This function will be called whenever a key is pressed
function keyPress(d) {
key = event.keyCode;
if(key==13) { //For Enter key
submitBtnObj[0].click(); 
}
}
</script>


The Problem: This works fine when on press of enter key, the button click is simulated. Hence if there is a javascript function that has to be called "onclick" of button it will be called. If the button type is "submit" the form is submitted. The problem is if "user" presses "Enter" key twice (say in quick succession) the button click is simulated twice. Hence if the button is submit, the form will be submitted twice. This happens only in IE (I have ver 6.0) and not in Mozzilla Firefox or Netscape Navigator.
Romil is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Old 04-26-2006, 08:34 AM
  #2
Catapulted
 
beefa's Avatar
 
Join Date: Mar 2004
Location: Yo Mamas House
Posts: 386
iTrader: ( 0)
beefa is on a distinguished road
Romil,

First off, a warm welcome to the forums.

Your problem certainly is a troublesome one. I think, however, I have the answer.

Slowing things down a bit, lets analyse the situation. Users are pressing enter twice rapidly. The browser will only run the script once after the other, and not at the same time. That means it will run the code, then run it again.

What we could do is after the submission has been activated, we disable the enter key. The script is run, it sees that the enter key has been pressed, it then submits the form, then disables the enter key. When it runs it again, it sees the enter key is disabled, and then does nothing, thus submitting the form once.

We can do this by setting a variable. Its much easier to set variables then it is to disable the key completely (and its also safer). Here is your new code:
Code:
<script language="javascript" type="text/JavaScript"">
var submitBtnObj = document.getElementsByName("saveButton");
document.onkeypress = keyPress;
var count = 1;
function keyPress(d) {
if (count == 1){
key = event.keyCode;
if(key==13) { //For Enter key
submitBtnObj[0].click();
var count=2;
}
}
}
</script>
In  theory it should work, good luck! 

Hope this helps,
Beefa
beefa is offline  
### 游标功能与用户身份验证 游标的本质是在数据库查询过程中用于管理和遍历返回的结果集的一种机制[^1]。然而,游标本身并不具备验证用户是否为人类的功能。这是因为游标的设计初衷是为了处理数据检索而非安全认证。 对于需要确认用户身份的应用场景,通常会采用专门的安全措施和技术手段来完成这一任务。常见的做法包括但不限于: - **验证码服务**:通过图形或音频形式向用户提供一次性挑战问题,要求用户输入正确答案以证明其不是自动化脚本。 - **多因素认证(MFA)**:除了传统的用户名密码组合外,还引入额外的身份验证因子,如短信验证码、电子邮件链接或是生物识别特征等。 - **行为分析技术**:利用机器学习模型监控用户的交互模式,基于历史行为构建正常活动档案,并实时检测异常登录尝试或其他潜在风险事件。 为了集成上述任一方案至现有应用程序中,建议按照如下方式调整架构设计: #### 实现示例:Python Flask 应用程序中的Google reCAPTCHA v3 集成 假设当前正在开发的是一个Web应用,则可以考虑使用reCAPTCHA这样的第三方API来进行机器人防护。以下是简化后的Flask框架下的实现代码片段: ```python from flask import Flask, request, jsonify import requests app = Flask(__name__) @app.route('/submit_form', methods=['POST']) def submit_form(): secret_key = 'your_secret_key_here' token = request.form.get('g-recaptcha-response') remote_ip = request.remote_addr payload = { 'secret': secret_key, 'response': token, 'remoteip': remote_ip } response = requests.post( "https://www.google.com/recaptcha/api/siteverify", data=payload ) result = response.json() if not result['success']: return jsonify({'error': 'Invalid CAPTCHA'}), 400 # 继续处理合法请求... return jsonify({'message': 'Form submitted successfully!'}) if __name__ == '__main__': app.run(debug=True) ``` 此段代码展示了如何接收前端传来的reCAPTCHA响应令牌并通过HTTPS POST请求发送给谷歌服务器进行校验。根据返回的结果判断此次提交是否来自于真实的人类访客。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值