独立的“未被发现” Chrome Console上的警报
我是在HTML,CSS和JavaScript中编码的新手,因此,如果我的代码凌乱和/或不正确,请原谅我。
我正在尝试制作一个可以帮助您在精神上感到沮丧的人的网站,对于首页,我添加了一个输入框,该框要求用户的名字,然后将在下一页中阅读。例如,如果我放“亨利”,下一页应该说“你好,亨利!”要读取下一页中的变量,我使用了SessionStorage和SessionStorage.getItem(“ InputVal”)。但是,由于某种原因,当我尝试在第一页中记录变量(InputVal)时,该控制台会出现一个错误,该错误简单地在短暂的时刻中简单地说“未被教”,然后在加载到下一页时清除。然后,当我尝试在下一页上记录InputVal时,控制台仅打印“ null”。我已经搜寻了许多形式,但是我找不到任何解决问题的东西!我写了sessionstorage,还是我缺少其他东西?这是我第一个文件的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
<title>Feel Happy</title>
</head>
<body>
<b class="welcometofeelhappy">Welcome to FeelHappy :)</b>
<input type="text", id="whatisyourname", placeholder="Please enter your name!", class="whatisyournameinput" required><br>
<form action="pages/landing.html">
<button class="startBtn", type="submit", id="btn1", onclick="getInputValue()"><h2 class="buttontext1">Let's Begin!</h2></button>
</form>
<script>
function getInputValue(){
var inputVal = document.getElementById("whatisyourname").value;
console.log(inputVal)
sessionStorage("inputVal", inputVal)
}
</script>
</body>
</html>
以及第二个文件的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="getName()" type="button">Click Me!!</button>
<script>
function getName() {
var inputVal = sessionStorage.getItem("inputVal")
console.log(inputVal)
}
</script>
</body>
</html>
注意:为了进行测试,我放了一个按钮将代码打印在控制台中。
I'm new to coding in HTML, CSS, and JavaScript, so please forgive me if my code is messy and/or incorrect.
I'm trying to make a website that can help people who are feeling mentally down, and for the first page, I added an input box which asks for the user's name, which will then be read in the next page. For example, if I put "Henry", the next page should say "Hello, Henry!" To read the variable in the next page, I used sessionStorage and sessionStorage.getItem("inputVal"). But, for some reason, when I try to log the variable (inputVal) in the first page, the console gives an error that simply says "Uncaught" for a brief moment before clearing when loading to the next page. Then, when I try to log the inputVal on the next page, the console only prints "null". I have scoured numerous forms, but I could not find anything to fix my problem! Have I written the sessionStorage wrong, or is there something else I'm missing? Here is the code for my first file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
<title>Feel Happy</title>
</head>
<body>
<b class="welcometofeelhappy">Welcome to FeelHappy :)</b>
<input type="text", id="whatisyourname", placeholder="Please enter your name!", class="whatisyournameinput" required><br>
<form action="pages/landing.html">
<button class="startBtn", type="submit", id="btn1", onclick="getInputValue()"><h2 class="buttontext1">Let's Begin!</h2></button>
</form>
<script>
function getInputValue(){
var inputVal = document.getElementById("whatisyourname").value;
console.log(inputVal)
sessionStorage("inputVal", inputVal)
}
</script>
</body>
</html>
And the code to the second file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="getName()" type="button">Click Me!!</button>
<script>
function getName() {
var inputVal = sessionStorage.getItem("inputVal")
console.log(inputVal)
}
</script>
</body>
</html>
Note: For testing purposes, I put a button to print the code in the console.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SessionStorage(“ InputVal”,InputVal)
不是您将内容写入会话存储的方式。SessionStorage
本身不是一个函数,它是提供方法的对象,例如setItem
。您需要
sessionstorage.setItem(“ InputVal”,InputVal)
。在调试方面,几乎可以肯定可以将浏览器的控制台配置为在页面过渡后持续存在日志,即使您远离发生错误的页面,您也可以看到错误消息。
sessionStorage("inputVal", inputVal)
is not how you write things to the session store.sessionStorage
itself is not a function, it's an object that provides methods, likesetItem
.You need
sessionStorage.setItem("inputVal", inputVal)
.In terms of debugging, your browser's console can almost certainly be configured to persist logs after page transitions, which will allow you to see error messages even after you navigate away from the page where the error occurred.