Link to home
Start Free TrialLog in
Avatar of pvg1975
pvg1975Flag for Argentina

asked on

DISABLE SUBMIT BUTTON AFTER CLICKED or ENTER KEY IS PRESSED

Hi guys!

Is there a way to disable a submit button after the form is submitted?

I am having problems when the user clicks on a button more than one, or when the user press the ENTER key more than once.

Thanks!

PVG
Avatar of archrajan
archrajan

<html>
<head>
<script type="text/javascript">
function handleEnter (field, event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
document.getElementById('sub').disabled = true;
//document.frm.submit();
}
}

</script>
</head>

<body onkeypress = "handleEnter(this,event)">
<form action="page2.html" name = "frm" onsubmit = "document.getElementById('sub').disabled = true">

<input type="submit" id = "sub" value="submit form">
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvg1975

ASKER

Hi guys,

Your solutions are great, but I still can press ENTER more than once.

I have the following script:

<script language="JavaScript">
function MyConfirm()
{
if (confirm("Confirm this operation?"))
      document.all.Submit1.disabled = true;
else
return false;
}
</script>

<form name="form1" method="post" action="po_view_canceltransaction.asp" onSubmit="if (MyConfirm()==false)return false; else return true;">

I used all your posted codes but it doesn't seen to work.

Again, the problem is when the user press ENTER repeatedly.

Thanks for your time guys.
Avatar of pvg1975

ASKER

In the code I posted above, is it possible to return FALSE if ENTER if pressed?.... in other words, allow the user to submit the form only by clicking with the mouse.
<script language="JavaScript">
function MyConfirm(theForm) {
 return (theForm.Submit1.disabled = confirm("Confirm this operation?"));
}
</script>
<form name="form1" method="post" action="po_view_canceltransaction.asp" onSubmit="return MyConfirm(this)">
<input type="submit" name="Submit1" value="Submit"/>
</form>
Get rid of this: document.all.Submit1.disabled = true;

It is IE only and nor specified anywhere in the W3C recommendations.

Use  archrajan's method which will also disable the Enter key if you add return !keyCode.
@ Your solutions are great, but I still can press ENTER more than once.

not with my solution
Avatar of pvg1975

ASKER

Thanks guys :)