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

时间:2014-12-10 17:31:27

标签: javascript

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

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

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

我能够将表单中的数据转换为函数,并具有创建昨天工作的收藏夹的功能,不知道今天早上发生了什么。

如果有人可以请看看这个并告诉我,如果我至少朝着正确的方向前进,我会很感激。我现在只是进入圈子。 (我在代码中有大量的console.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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

把这个:

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                    
  }