【发布时间】:2018-10-10 02:34:51
【问题描述】:
我正在为学校做一个项目。在项目中,我需要调用一个自制的 toString 来显示我的构造函数的所有键和值。我应该填写一张表格并将我的电脑存储在 localStorage 中。
我的逻辑函数
function Logiciels(ordiConfigure){
//I start at one so I dont't get the first input which is text type
for (var i = 1; i < document.forms[2].getElementsByTagName("input").length; i++) {
if(document.forms[2].getElementsByTagName("input")[i].checked == true)
ordiConfigure.Logiciels[i] = document.forms[2].getElementsByTagName('input')[i].value;
ordiConfigure.Logiciels = ordiConfigure.Logiciels.filter(valeur => valeur !== null);
return ordiConfigure.Logiciels;
}
我如何存放电脑
var ordiConfigure = new pcConfig();
ordiConfigure.Logiciels = Logiciels(ordiConfigure);
localStorage.setItem("ordinateur", JSON.stringify(ordiConfigure));
我的 toString()
pcConfig.prototype.toString = function() {
var caracteristiques = "<ul>";
for (var proprieteOrdinateur in this){
if(typeof(this[proprieteOrdinateur]) !== "function")
caracteristiques += "<li>" + proprieteOrdinateur + " : " + this[proprieteOrdinateur] + "</li><br/>";
}
caracteristiques += "</ul>";
return caracteristiques;
};
这里是构造函数
function pcConfig(){
this.Taille = document.forms[2].getElementsByTagName('select')[1].value;
this.Systeme = document.forms[2].getElementsByTagName('select')[0].value;
this.Identifiant = document.forms[2].getElementsByTagName('input')[0].value;
//This an array for the softwares chosen
this.Logiciels = logiciels;
}
我是这样称呼它的
<script>
document.getElementsByTagName('div')[0].innerHTML = JSON.parse(localStorage.getItem("ordinateur")).toString();
</script>
picture of the page which possesses the form
它返回null,我不知道为什么?任何帮助将不胜感激。
【问题讨论】:
-
旁注,不要像
document.forms[2].getElementsByTagName('input')这样不断地打电话。将其存储在变量中并使用该变量
标签: javascript json object local-storage tostring