テキストボックスがテキストで埋められているかどうかを検証しようとしています。文字が入力されていない場合、アラート(alert("Name must be filled out");
)はうまく機能しますが、それ以外の場合は、一部の文字が入力されたとき(else window.location.href = 'http://www.w3schools.com';
)URLに転送されません。JavaScriptを使用してURLにナビゲートする方法
いくつかの方法を教えてください。前もって感謝します。
<html>
<head>
<script>
function validateForm() {
var x = document.forms["registration"]["name"].value;
if (x == null || x == "") {
alert("Name must be filled out");
\t return false;
\t } else window.location.href = 'http://www.w3schools.com';
}
</script>
</head>
<body>
<form name="registration" method="post" >
Name <input type="text" name="name" />
<br>
<button type="Submit" name="submit" onClick="validateForm()" >Submit</button>
</form>
</body>
</html>
をボタン「タイプ」を変更することで問題を解決し、それは私が@Lokuztに同意男 – Lokuzt
の作品スニペット。できます。 –
中括弧をより一貫して使用することをお勧めします。つまり、 'else'節のほかに' if'節のまわりにそれらを持っています。 –