如果浏览器中禁用了 JavaScript,如何禁用 HTML/JavaScript 表单

首先,我对此很陌生,我真的尽力了解它是如何工作的。我有以下简单的登录表单,如果提交正确的登录凭据,该表单将进入主页。然而,当在禁用 JavaScript 的 Firefox 中运行时,登录凭据将被忽略,并且无论我提供什么登录详细信息,我的主页都会显示。我创建了一条消息,<noscript></noscript>当 JavaScript 被禁用时会在该消息之间触发。我想要实现的是,仅显示警告消息,并且表单等被禁用,并且在启用 JavaScript 的情况下重新加载登录页面之前不会显示。有人可以帮我解决这个问题吗?非常感谢!我的代码是


<!doctype html>

<!-- This is is the main start page saved in index.html -->

<html>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<body>


<h2>Login details</h2>


<noscript>

        <h3>Before you login</h3>

        <p>JavaScript needs to run for this site to work properly.<br />

        Please activate JavaScript in your browser<br />

        and reload this page to login.

        </p>

</noscript>


<form id="myForm" name="login" onsubmit="return validateFormOnSubmit()" action="http://localhost:8080/index.html" method="post">

<fieldset>

  <label for="username">Username:</label><br>

  <input type="text" id="username" name="username"><br>

  <label for="password">Password:</label><br>

  <input type="password" id="password" name="password"><br><br>

  <input type="submit" value="Submit">

 </fieldset>

</form>

</body>

</html>


<script>

        function validateFormOnSubmit() {

        var un = document.login.username.value;

        var pw = document.login.password.value;

        var username = "username"

        var password = "password"

        if ((un == username) && (pw == password)) {

                return true;

        }

        else {

                alert ("Login was unsuccessful, please check your username and password");

                return false;

        }

}

</script>


芜湖不芜
浏览 75回答 1
1回答

慕尼黑5688855

您无需添加额外的 JavaScript 即可实现此目的。<noscript>您也可以在 中使用标签<head>,这样您就可以使用 CSS 隐藏它:…<head>&nbsp; &nbsp;…&nbsp; &nbsp;<noscript>&nbsp; &nbsp; &nbsp; &nbsp;<style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#myForm {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display: none&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;</style>&nbsp; &nbsp;</noscript></head>…
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5