【问题标题】:Why does my object not alert when I've made it, saved it, and want to alert it?为什么我的对象在我制作、保存并想要提醒它时没有提醒?
【发布时间】:2021-10-22 06:23:58
【问题描述】:

我正在编写一个代码,用户输入信息并将其存储在一个对象中,但是当我通过警告它的一个值来检查对象是否被保存时,它说没有这样的对象。 这是我的代码。

let users = {};

function user(name,real){
  this.username = name,
  this.realname = real,
  this.id = Math.floor(Math.random() *1000);
  this.subs = 0,
  this.videos = [],
  this.listuser = function() {
    return this.username;
  }
};
function newuser(name, email,username){
  users[name] = new user(username, name);
};


let savefile = () => {
        
  var channame = string(document.getElementById('channame'));
  var name = string(document.getElementById('name'));
  var email = string(document.getElementById('email'));
  newuser(name,email,channame);
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>youtube ripoff</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
  <br> <br><br><br><br><br>
  <form action="">
  <label for="username">Channel Name: </label>
  <input type="text" id="channame" name="channelname"><br>
  Real Name
  <input type="text" id="name" name="name"><br>
  Email
  <input type="text" id="email" name="email"><br>
  <input type="submit" value="Submit" onmousedown="newuser('name1','name@gmail.com','channelname1');">
  <br><br><br><br>
  
  <input type="button" value="Load Channels" id="seech" name="seechannels" onmousedown="alert(users['name1'].listuser());"><br>
  </form>
    <script src="script.js">
    function fun(){
      window.alert("starting program...")
      //other code
    }
    </script>
  </body>
</html>

是否有某种我没有看到的错误?

【问题讨论】:

标签: javascript html object


【解决方案1】:

点击表单中带有 action="" 的提交按钮后,页面会重新加载,所有数据都会丢失。请使用:

<form action="javascript:void(0)">

而不是

<form action="">

这可以防止页面重新加载。

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    相关资源
    最近更新 更多