简体   繁体   English

我如何创建一个 javascript function 在按下表单提交按钮时更改 html

[英]how do I create a javascript function that changes the html when a form submit button is pushed in django

I can easily update the html when it's not part of the form's submit or button.当 html 不是表单提交或按钮的一部分时,我可以轻松更新它。 Or even when it's just a pure button element (rather than an input from a form).或者即使它只是一个纯按钮元素(而不是来自表单的输入)。 However, when I try to append a string to the但是,当我尝试 append 一个字符串到

class "chatGoesHere", nothing happens. class "chatGoesHere",没有任何反应。 The consolealso quickly reloads since the form is going to \send.控制台也会快速重新加载,因为表单将要发送到 \send。

I'm happy to post my views.py and urls.py, however, I'm pretty sure the issue is inside of my html document below:我很高兴发布我的 views.py 和 urls.py,但是,我很确定问题出在下面的 html 文档中:

<p class="chatGoesHere" id="chatGoesHere"> 1st Item! </p> 

<form action="\send\" method="post">
    <input type="text" name="userMessage" />
    <input type="submit" value="Send to smallest_steps bot" class="sendButt" id="sendButt" />
  </form>
  
  <script>
      var btn = document.getElementById("sendButt");
      btn.addEventListener("click", updateChat);
  
      function createMenuItem(name) {
          let li = document.createElement('p');
          li.textContent = name;
          return li;
      }
      const td = document.getElementById('chatGoesHere');
      td.appendChild(createMenuItem("TEST2"))
  
      function updateChat(){
          const td = document.getElementById('chatGoesHere');
          td.appendChild(createMenuItem("TEST3"))
      }
  </script>

I'd like it so that every time a user pushes the submit button of the form something gets added to the page without the page reloading.我希望每次用户按下表单的提交按钮时,都会在页面中添加一些内容,而无需重新加载页面。

Thank you谢谢

You need to use django with sockets.您需要将 django 与 sockets 一起使用。

Take a look at this walk through.看看这个走过。

Helped me to do the same thing a few years ago!几年前帮助我做同样的事情!

https://channels.readthedocs.io/en/stable/tutorial/part_2.html https://channels.readthedocs.io/en/stable/tutorial/part_2.html

暂无
暂无

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

相关问题 如何在Javascript / HTML计算表单上实现“提交”按钮? - How do I implement a “Submit” button on a Javascript/HTML calculation form? 没有提交按钮的情况下如何向JavaScript提交表单? - How do I submit a form to JavaScript without the submit button? 如何使用 JavaScript 函数向 HTML 表单添加提交按钮? - How to add submit button to an HTML form with JavaScript function? 如何在类似于onsubmit =“return javascript function”的FORM中的提交按钮上执行php函数? - How do I execute php function on submit button in a FORM similar to onsubmit=“ return javascript function”? 我有一个带有链接到javascript函数的提交按钮的表单,但是当我单击提交按钮时没有任何反应 - I have a form with the submit button linked to javascript function but nothing is happening when I click the submit button 如何在 html 表单中使用 JS function 控制提交按钮 - how can i control the submit button with JS function in html form Rails + Javascript:如何创建一个从左到右滑动/更改表单的按钮? - Rails + Javascript: How do I create a button that slides/changes a form from left to right? 如何创建将提交到Web表格php的电子邮件按钮? - How do i create an email button that will submit to web form php? 如何使用提交按钮创建一个文本框,使用js或jQuery单击javascript函数执行回调? - How do I create a text box with a submit button that performs a callback on click to a javascript function using js or jQuery? 仅当推送提交时才运行JavaScript函数吗? - Run JavaScript Function ONLY when Submit is Pushed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM