简体   繁体   English

如何从文本字段中获取用户输入并将其显示在网页上?

[英]How do I get user input from text fields and display it on the webpage?

I'm learning how to code js and one of my goals is to have a pseudo commenting system on my webpage.我正在学习如何编写 js 代码,我的目标之一是在我的网页上拥有一个伪评论系统。 I'm wondering how to make javascript that can make it so that the user can type a comment and it'll display on the webpage.我想知道如何制作 javascript 以便用户可以输入评论并将其显示在网页上。 I've tried using getelementbyclassname but because I'm a beginner I don't really know what I'm doing.我试过使用 getelementbyclassname 但因为我是初学者我真的不知道我在做什么。 HTML: HTML:

<div class="commentform">
             <h1>Leave your comment!</h1>
             <br>
             <br>
             <form class="commentform" method="post">
                 <p>Nickname or Name</p> <input id="name" required="required" type="text">
                 <br>
                 <p>Comments: </p><textarea id="comment" name="comments" rows="8" cols="20"></textarea>
                 <button type="button" name="commentsubmit">Comment!</button>
            </form>
</div>

So you want the comment to pop up when the user submits the form.因此,您希望在用户提交表单时弹出评论。 A problem I noticed about your code is that <button type="button" name="commentsubmit">Comment!</button> doesn't actually submit the form - it should have type="submit" .我注意到您的代码的一个问题是<button type="button" name="commentsubmit">Comment!</button>实际上并没有提交表单 - 它应该有type="submit"

Also, you don't specify where on the webpage you want the comment to be placed, so I'll assume it's some div, I'll use <div id="target"></div> but it could be anything.此外,您没有指定要将评论放置在网页上的哪个位置,所以我假设它是一些 div,我将使用<div id="target"></div>但它可以是任何东西。

First: triggering JS when the form is submitted一:表单提交时触发JS

You can use the .addEventListener function to call a function when the form is submitted.提交表单时,您可以使用 .addEventListener .addEventListener调用 function。 It seems like you are having some trouble selecting the form though.不过,您似乎在选择表格时遇到了一些麻烦。 One way is to use document.querySelector , which is more versatile than getElementsByClassName , since you have more than one element with the same class name.一种方法是使用document.querySelector ,它比getElementsByClassName更通用,因为您有多个具有相同 class 名称的元素。 We want to select a form with class commentform , which can be represented as form.commentform (look familiar? this is also a CSS selector.).我们想要 select 一个带有 class commentformform ,可以表示为form.commentform (看起来很熟悉?这也是一个 CSS 选择器。)。

document.querySelector("form.commentform").addEventListener("submit", function(event) {

});

This selects the form, and adds a listener that triggers a function when the form is submitted.这将选择表单,并添加一个侦听器,该侦听器在提交表单时触发 function。 We can place code inside of this function body.我们可以在这个 function 主体中放置代码。

Second: putting the comment on the page二:在页面上放评论

We'll need to retrieve the comment first.我们需要先检索评论。 The comment is in a textarea with id="comment" - perfect, we can use document.getElementById("comment") to select it.评论在id="comment"的文本区域中 - 完美,我们可以使用document.getElementById("comment")到 select 它。 Then, we can call .value on the textarea to retrieve its contents.然后,我们可以在 textarea 上调用.value来检索其内容。

document.getElementById("comment").value

Then, we can set the contents of the <div id="target"></div> to the comment.然后,我们可以将<div id="target"></div>的内容设置为评论。 This can be done the same way as we retrieved the textarea, except we use .innerText instead of .value .这可以与我们检索 textarea 相同的方式完成,除了我们使用.innerText而不是.value

document.getElementById("target").innerText = document.getElementById("comment").value;

This now sets the text of the target div to equal the comment that was entered.这现在将目标 div 的文本设置为等于输入的注释。 If you also want the page to not refresh when the user submits the form, you'll want to put event.preventDefault();如果您还希望在用户提交表单时页面不刷新,您需要放置event.preventDefault(); in the function as well, to tell the browser to prevent the default behavior of submitting a form (refreshing the page).在 function 中也是如此,告诉浏览器阻止提交表单的默认行为(刷新页面)。

 document.querySelector("form.commentform").addEventListener("submit", function(event) { event.preventDefault(); document.getElementById("target").innerText = document.getElementById("comment").value; });
 <div class="commentform"> <h1>Leave your comment:</h1> <br> <br> <form class="commentform" method="post"> <p>Nickname or Name</p> <input id="name" required="required" type="text"> <br> <p>Comments: </p><textarea id="comment" name="comments" rows="8" cols="20"></textarea> <button type="submit" name="commentsubmit">Comment!</button> </form> </div> <div id="target"></div>

If you also want to display the nickname, the steps are similar.如果还想显示昵称,步骤类似。 I'll leave that as an exercise for you.我会把它留给你作为练习。

All what you need to do is to add a script tag and within it make a function where you can get the value of what your're typing within the text field, add the onclick to the button so that you invoke the function.您需要做的就是添加一个脚本标签并在其中创建一个 function ,您可以在其中获取您在文本字段中输入的值,将 onclick 添加到按钮,以便您调用 ZC1C425268E68394F1C14

<div class="commentform">
             <h1>Leave your comment!</h1>
             <br>
             <br>
             <form class="commentform" method="post">
                 <p>Nickname or Name</p> <input id="name" required="required" type="text">
                 <br>
                 <p>Comments: </p><textarea id="comment" name="comments" rows="8" cols="20"></textarea>
                 <button onclick='getText()' type="button" value="hello" name="commentsubmit">Comment!</button>
            </form>
  <p id = 'demo'> </p>
</div>
<script>
  function getText (){
  var x = document.getElementById("comment").value
   document.getElementById("demo").innerHTML = "You commment is : " + x;
  console.log(x)
}
</script>

You can do something like this....你可以做这样的事情......

 const handleComment = (e)=>{ e.preventDefault(); const newDiv = document.createElement("div"); newDiv.innerText = e.target.comments.value; const comments = document.getElementById("comments") comments.append(newDiv) } const form = document.getElementById("commentForm") form.addEventListener("submit", handleComment, true);
 <div id="comments"></div> <div class="commentform"> <h1>Leave your comment:</h1> <br> <br> <form id="commentForm" class="commentform" > <p>Nickname or Name</p> <input id="name" required="required" type="text"> <br> <p>Comments: </p><textarea id="comment" name="comments" rows="8" cols="20"></textarea> <button type="submit" name="commentsubmit">Comment!</button> </form> </div>

What I did was added an event listener to the form that captures the submit event.我所做的是将一个事件侦听器添加到捕获提交事件的表单中。

I then took the comment text and used innerText to add it to a dynamically created div (using document.createElement).然后我获取注释文本并使用 innerText 将其添加到动态创建的 div 中(使用 document.createElement)。

Finally I appended the div to another div I created and gave an Id of comments.最后,我将 div 附加到我创建的另一个 div 并给出了评论的 ID。 (inside the HTML) (在 HTML 内)

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

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