[英]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.