0
<script>
function validate(){
u=document.CustomerLogin.UserName.value;
p=document.CustomerLogin.Password.value;
rp=document.CustomerLogin.RePassword.value;

if (u==""){
 document.write("Enter a unique username");
 return false;
 }
if (p!=rp){
 document.write("Retype Password Incorrect");
 return false;
 }
return true;
}
</script>

メッセージは別のページに印刷されますが、テキストボックスの前の同じ場所に印刷したいです! 助けてください。ありがとう!

4

3 に答える 3

1

Document write は絶対に使用しないでください。危険です...

document.getElementById("anidofanElementinthePage").innerHTML = "javascript で追加する文字列";

<script>
function validate(){
u=document.CustomerLogin.UserName.value;
p=document.CustomerLogin.Password.value;
rp=document.CustomerLogin.RePassword.value;

if (u==""){
  document.getElementById("theIdOfyourTextBlocInForm").innerHTML = "Enter a unique username";
 return false;
 }
if (p!=rp){
 document.getElementById("theIdOfyourTextBlocInForm").innerHTML = "Retype Password Incorrect";
 return false;
 }
return true;
}
</script>
于 2012-05-24T21:49:17.880 に答える
0

document.write は単にメッセージをページの最後に追加します。最も簡単な解決策は、次のようなアラートに変更することです。

if (u==""){
 alert("Enter a unique username");
 return false;
}

それ以外の場合は、メッセージを保持して入力の隣に配置するために、(事前に、またはエラーが検出されたときに) 新しい DOM 要素を作成する必要があります。

于 2012-05-24T21:51:06.803 に答える
0

テキストの変更を検出する必要があります。1 つの方法は、これを使用することです。

<input name="blah" onchange="validate()" ... />

編集 同じページにテキストを追加するには、スクリプトを使用して含め、各入力の隣に<script src=...>追加します。毎回 1 ずつ増加します (validation1 validation2 など)。次に、 を使用して値を変更します。<div id='validation[i]'></div>[i]getElementById('validation[i]').innerHtml

于 2012-05-24T21:43:03.283 に答える