javascript - 意见插入数组后div不刷新
问题描述
我在我的数组中插入了一些值(插入后在控制台中查看数组和输出)但 html 内容不刷新(我必须按 F5 然后内容出来)。感谢您的帮助(我正在使用 jquery/js)。
if(localStorage.myTreesComments){
opinions=JSON.parse(localStorage.myTreesComments);
}
console.log(opinions);
const myFrmElm=document.getElementById("opnFrm");
myFrmElm.addEventListener
("submit",processOpnFrmData);
/**
* initialisation of the list of visitor opinions and form submit setup
*/
function init(){
if (localStorage.myTreesComments) {
this.opinions = JSON.parse(localStorage.myTreesComments);
}
this.opinionsElm.innerHTML = this.opinionArray2html(this.opinions);
this.opinionsFrmElm.addEventListener("submit", event => this.processOpnFrmData(event));
}
function processOpnFrmData(event) {
//1.prevent normal event (form sending) processing
event.preventDefault();
//2. Read and adjust data from the form (here we remove white spaces before and after the strings)
const nopName = document.getElementById("menoapriezvisko").value.trim();
const nopEmail = document.getElementById("email").value.trim();
const nopUrl = document.getElementById("url").value.trim();
const nopComm = document.getElementById("komentar").value.trim();
const nopSlovo = document.getElementById("klucoveslovo").value.trim();
const nopWillReturn = document.getElementById("willReturnElm").checked;
//3. Verify the data
if(nopName=="" || nopEmail==""|| nopUrl==""|| nopComm==""|| nopSlovo==""){
window.alert("Prosím, zadajte všetky údaje.");
return;
}
//3. Add the data to the array opinions and local storage
const newOpinion =
{
name: nopName,
email: nopEmail,
url: nopUrl,
comment: nopComm,
gdpr: nopWillReturn,
klucove_slovo: nopSlovo,
created: new Date()
};
console.log("Nový názor návštevníka:\n "+JSON.stringify(newOpinion));
opinions.push(newOpinion);
localStorage.myTreesComments = JSON.stringify(opinions);
//4. Notify the user
window.alert("Tvoj názor bol uložený a pridaný na stránku!");
console.log("Nový názor bol pridaný!");
console.log(opinions);
//4. Update HTML
this.opinionsElm.innerHTML+=this.opinion2html(newOpinion);
//5. Reset the form
myFrmElm.reset(); //resets the form
/*this.opinionsFrmElm.reset(); //resets the form*/
}
/**
* creates html code for one opinion using a template literal
* @param opinion - object with the opinion
* @returns {string} - html code with the opinion
*/
function opinion2html(opinion){
const opinionTemplate=
`
<section>
<h3>${opinion.name} <i>(${(new Date(opinion.created)).toDateString()})</i></h3>
<p>${opinion.comment}</p>
<p>${opinion.willReturn?"I will return to this page.":"Sorry, one visit was enough."}</p>
</section>`;
return opinionTemplate;
}
/**
* creates html code for all opinions in an array using the opinion2html method
* @param sourceData - an array of visitor opinions
* @returns {string} - html code with all the opinions
*/
function opinionArray2html(sourceData){
return sourceData.reduce((htmlWithOpinions,opn) => htmlWithOpinions+ this.opinion2html(opn),"");
}
</script>
<script type="module" src="js/myTreesFormLStorageHtmlWr.js"></script>
<script type="module" src="js/opinionsHandlerMustache.js"></script>
<script id="mTmplOneOpinion" type="text/template">
<section>
<h3>{{name}} <i>{{createdDate}}</i></h3>
<p>{{comment}}</p>
<p>{{willReturnMessage}}</p>
</section>
</script>
只需在 HTML 页面中,它应该是从数组中查看名称和注释元素并自动刷新。谢谢
解决方案
推荐阅读
- docker - 当我运行 docker-compose up -d 错误服务必须是映射窗口缩进错误
- javascript - 随着孩子数量的增加,防止 div 的高度增加
- windows - Windows Cmd findstr - 带有数字的正则表达式不起作用
- sql-server - 如何在 SSMS 的一张表中显示这 3 个查询结果?
- powershell - Get-PrintConfiguration 输出到文件和屏幕
- docker - 在 Windows 10 上构建 Linux Docker 映像:“exec 格式错误”
- algorithm - 计算将一行球放入盒子中的方法数量的算法
- c++ - 创建 1 到 20 之间的 5 个唯一整数的数组的算法
- javascript - FirebaseError:collection() 的第一个参数应为 CollectionReference、DocumentReference 或 FirebaseFirestore
- active-directory - 2021 年在 Windows 10 上安装 Active Directory 用户和计算机 (ADUC)