JavaScript 新手 - 需要帮助使用表单中的数据创建多个对象

New to JavaScript - Need help creating multiple objects with data from a form

本文关键字:数据 创建 对象 表单 新手 帮助 JavaScript      更新时间:2023-09-26

我刚开始学习JavaScript,并且已经坚持了大约一周的作业,我已经尝试了几种解决方案,但每次添加一些东西时,我都会破坏其他东西。任务是创建一个表单(我已经完成(。该表单用于输入新收藏夹项目的数据,输入每个项目时,它应显示在页面和控制台日志中,当输入其他项目时,它们将添加到列表中并显示所有项目-

例如 - 收藏夹是: 网址:http:/

/oriellyschool.com, 标题: O'Reilly School, 评论: 帮助, 标签: 学校, 学习, 网址: http://google.com, 标题: 谷歌, 评论: 使用谷歌查找有关 JavaScript 的信息, 标签: 搜索, 查找信息收藏夹:html:133

具体说明是对所有收藏夹和每个新的收藏夹项目使用对象。显示收藏夹(在控制台和页面上(的功能应包含在对象的方法中。

我能够将数据从表单获取到一个函数中,并且具有创建收藏夹的功能,昨天工作,不确定今天早上发生了什么。

如果有人能看看这个,告诉我我是否至少朝着正确的方向前进,我将不胜感激。我现在只是在兜圈子。(我在代码中有大量的控制台.log语句,所以我可以尝试查看它在做什么(。谢谢!

法典:

//  Function to create entries for favorite items from web form
function FaveoriteEntry(url, title, comment, tags) {
  console.log("In Fave Function");
  this.url = url;
  console.log(this.url);
  this.title = title;
  console.log(this.title);
  this.comment = comment;
  console.log(this.comment);
  this.tags = tags;
  console.log(this.tags);
  console.log("Have all items");
}
//Function to retrieve data from web form and send to function to creat favorite   
//object.     
function addFavorite() {
  var myFavorites = [];
  console.log("In Function");
  var furl = getFavorites.formURL.value;
  var ftitle = getFavorites.formTitle.value;
  var fcomment = getFavorites.formComment.value;
  var ftags = getFavorites.formTags.value;
  this.clear = getFavorites.reset();
  console.log("Entry: " + furl + " " + ftitle + " " + fcomment + " " + ftags);
  this.createFavorites = function(url, title, comment, tags) {
    console.log("In Fave Function");
    this.url = url;
    console.log(this.url);
    this.title = title;
    console.log(this.title);
    this.comment = comment;
    console.log(this.comment);
    this.tags = tags;
    console.log(this.tags);
    console.log("Have all items");
    this.string = (this.url + "," + this.title + "," + this.comment + "," +
      this.tags);
    myFavorites.push(this.string);
    var addfavorite = new this.createFavorites(furl, ftitle, fcomment,
      ftags);
    console.log(myFavorites);
  }
}
body {
    font-family: Helvetica, Ariel, sans-serif;
}
form {
    display: table;
    border-spacing: 5px;
}
form p {
    display: table-row;
}
form label {
    display: table-cell;
    text-align: right;
}
form input {
    display: table-cell;
}
span.comment {
    font-size: 80%;
    color: #777777;
}
span.tags {
    font-size: 80%;
    color: rgb(48, 99, 170);
}
<!doctype html>
<html>
<head>
  <title>Advanced JavaScript Project: Favorites and Tags</title>
  <meta charset="utf-8">
</head>
<body>
  <form name="getFavorites" onsubmit="return favorites(this)">
    <h1>Tag and save your favorites</h1>
    <text></text>
    <fieldset>
      <legend>Add a new favorite:</legend>
      <p>
        <label>URL:</label>
        <input type="text" name="formURL" value="" />
        <p>
          <label>Title:</label>
          <input type="text" name="formTitle" value="" />
        </p>
        <p>
          <label>Comment:</label>
          <input type="text" name="formComment" value="" />
        </p>
        <p>
          <label>Tags:</label>
          <input type="text" name="formTags" value="" />
        </p>
        <input type="button" name="button" value="Add Link" onClick="addFavorite(this.form)" />
    </fieldset>
    <u1 id="faves-lists">
      <h1> List of Favorites</h1>
      <li>Test -</li>
      <p> <span class="comments"></span>
      </p>
      <p> <span class="tags"></span>
      </p>
    </u1>
  </form>
</body>
</html>

这样说:

var addfavorite = new this.createFavorites(furl, ftitle, fcomment,
            ftags);             
console.log(myFavorites);      

createFavorites函数的外侧,如下所示:

function addFavorite() {
    var myFavorites = [];
    console.log("In Function");
    var furl = getFavorites.formURL.value;
    var ftitle = getFavorites.formTitle.value;
    var fcomment = getFavorites.formComment.value;
    var ftags = getFavorites.formTags.value;
    this.clear = getFavorites.reset();
    console.log("Entry: " + furl + " " + ftitle + " " + fcomment + " " + ftags);
    this.createFavorites = function(url, title, comment, tags) {
         console.log("In Fave Function");
         this.url = url;
         console.log(this.url);
         this.title = title;
         console.log(this.title);
         this.comment = comment;
         console.log(this.comment);
         this.tags = tags;
         console.log(this.tags);
         console.log("Have all items");
         this.string = (this.url + "," + this.title + "," + this.comment + "," + 
                        this.tags); 
         myFavorites.push(this.string);          

      }   
    var addfavorite = new this.createFavorites(furl, ftitle, fcomment,
            ftags);             
console.log(myFavorites);  // result here                    
  }