Il modulo viene inviato anche se JavaScript divertente restituisce false
-
06-07-2019 - |
Domanda
Il modulo .html del codice di controllo di Pls viene inviato anche se javascript restituisce false.
<form id="form1" name="form1" method="post" action="sub.jsp" onsubmit="return getValue()">
<input type="text" id="userName" name="userName" onkeyup="return getValue()" />
<input type="submit" name="Submit" value="Submit" />
</form>
<script type="text/javascript" >
function getValue()
{
var userName=document.getElementById("userName");
document.getElementById("userNamemsg").innerHTML="";
if(userName.value=="")
{
var mdiv=document.getElementById("userNamemsg");
mdiv.innerHTML="Error:Required Fields cannot be blank!";
form.userName.focus();
return false;
}
return true;
}
Soluzione
1) prova a cambiare la riga form.userName.focus();
in document.form1.userName.focus ();
O
2) prova a inviare dalla stessa funzione:
<input type="button" name="Submit" value="Submit" onclick="getValue()" />
<script type="text/javascript" >
function getValue()
{
var userName=document.getElementById("userName");
document.getElementById("userNamemsg").innerHTML="";
if(userName.value=="")
{
var mdiv=document.getElementById("userNamemsg");
mdiv.innerHTML="Error:Required Fields cannot be blank!";
document.form1.userName.focus();//I think this is the problem
return false;
}
document.form1.submit();
}
</script>
Altri suggerimenti
Penso che ci siano errori nel tuo codice JavaScript che si verificano prima delle dichiarazioni di reso. Correggi quegli errori e il tuo codice dovrebbe funzionare.
in alternativa, fai il gestore dei clic sul pulsante di invio per restituire false.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow