简体   繁体   English

新输入替换而不是追加

[英]new input replace Instead of append

Here is the html that I wrote for program这是我为程序编写的 html

It's working but append is not working.它正在工作,但附加不起作用。

 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container m-5"> <div class="row"> <div class="col-md-6 justify-content-center"> <span>Full Name :</span> <input id="name" type="name" class="d-flex mb-3"> <span>Email :</span> <input id="email" type="email" name="email" class="d-flex mb-3"> <span>Comment :</span> <input id="comment" type="text" class="d-flex mb-3"> <button id="btn" class="mt-3">Submit</button> </div> <div class="col-md-6"> <p id="demo"></p> </div> </div> </div>

name and comment are inputs.名称和评论是输入。

I type some text but append doesn't working and the new text that entered replace the first one.我键入了一些文本,但追加不起作用,输入的新文本替换了第一个文本。

 function SubmitComment(name, comment) { let newComment = censor(comment) for (let i = 0; i < name.length; i++) { let demo = $("#demo"); demo.html("") demo.append(` <h4>${name.val()} :</h4> <br> <p>${newComment}</p> `) } } function censor(comment) { var splitString = comment.val().split(" ") for (let b = 0; b < splitString.length; b++) { if (splitString[b] == "duck") { splitString[b] = '****'; } if (splitString[b] == "swan") { splitString[b] = '****'; } } var joinArray = splitString.join(" "); return joinArray; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Am not sure why your looping the name property, but here is a working example!我不确定为什么要循环 name 属性,但这是一个工作示例!

 function Submit() { SubmitComment($('.name'), $('.comment')); } function SubmitComment(name, comment) { let newComment = censor(comment) let demo = $("#demo"); demo.html("") for (let i = 0; i < name.length; i++) { demo.append(` <h4>${name.val()} :</h4> <br> <p>${newComment}</p> `) } } function censor(comment) { var splitString = comment.val().split(" ") for (let b = 0; b < splitString.length; b++) { if (splitString[b] == "duck") { splitString[b] = '****'; } if (splitString[b] == "kilt") { splitString[b] = '****'; } } var joinArray = splitString.join(" "); return joinArray; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="name" /> <input class="comment" /> <button onClick="Submit()">Submit</button> <div id="demo"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM