JavaScript 新手 - 需要帮助使用表单中的数据创建多个对象
New to JavaScript - Need help creating multiple objects with data from a form
我刚开始学习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
}
相关文章:
- 使用外部数据创建仪表板(谷歌电子表格)-JavaScript
- 从html表中的输入数据创建图表
- 使用来自不同循环Javascript的数据创建数组
- 在d3forjavascript中,如何为数据创建不同的元素
- 如何使用json数据创建下拉列表
- 使用 mysql 数据创建条形图
- 在javascript中使用给定的数据创建Json
- Jquery:为新数据创建寻呼机(next-prev按钮)
- 为Meteor数据创建编号列表
- 如何创建和下载PDF和docx格式的文件,该文件将从AngularJS中的表中的数据创建
- 为数据创建唯一变量
- 如何使用旧数组中的数据创建新数组
- 使用外部数据创建体式任务
- 将数据从一个工作表复制到另一个工作表;使用目标工作表中的复制数据创建一个表
- 我可以在谷歌地图上使用OpenStreetMap数据创建获取路线吗?如何
- 如何为具有嵌套对象的 JSON 数据创建 Mustache 模板
- 如何使用 import.io 中的数据创建 Web 应用
- 从 CSV 数据创建 D3.js 可折叠树
- 如何使用SQLite数据创建Javascript var
- 使用 AJAX 数据创建注释页选择