【发布时间】:2022-01-05 21:59:28
【问题描述】:
我正在尝试在提交表单时显示一个 div。它在提交表单时显示 div,而未提交表单。这里使用 POST 方法。 JavaScript 用于显示 div。
html 文件:
<form method="POST" name="myFirstForm" id="chform">
{% csrf_token %}
<input type="hidden" name="details_id" value="{{details.id}}"/>
<input type="hidden" name="details_user_id" value="{{details.user_id}}"/>
<input type="hidden" name="user_id" value="{{user.id}}"/>
<input type="submit" class="btn btn-danger" id="chat" onclick="return myFunction()" value="Chat Now">
</form>
<div class="page-content page-container" id="page-content" style="display:none"></div>
JavaScript:
<script>
document.forms['myFirstForm'].addEventListener('submit', function(event) {
// Do something with the form's data here
this.style['display'] = 'none';
event.preventDefault();
});
function myFunction() {
var x = document.getElementById("page-content");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
views.py:
if request.method=='POST':
jid = request.POST.get('details_id')
print(jid)
cjid = request.POST.get('details_user_id')
print(cjid)
userid = request.POST.get('user_id')
print(userid)
如果我不想显示 div 并删除 JavaScript 代码,POST 方法可以工作。有人可以提出解决此问题的解决方案吗?
【问题讨论】:
标签: javascript python-3.x django post