Form is submitted twice when Enter key is pressed twice

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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值