javascript - 从 js 脚本中检索输入值到 django 视图
问题描述
我以这种方式在 js 脚本中创建了一个 html 输入:
var compte = document.createElement("input");
compte.setAttribute("name", "compte");
我需要将该输入值检索到 django 视图中。通常,我使用输入的名称属性:
num=request.POST['compte']
在这种情况下,知道 html 输入是使用 javascript 创建的,它不起作用。那么我如何获得价值呢?
我在整个代码下面添加。我实际上已经插入了一个包含表输入的新行。
<form class="myForm" action="Plan_comptable" method='POST'
enctype="multipart/form-data">
<table id="myTable" class="gtreetable table gtreetable-fullAccess">
{% csrf_token %}
<thead><tr><th>Compte</th><th>Intitulé</th><th></th>
</tr></thead>
<tbody>
{% for el in ViewPC_query %}
<tr>
<td>{{ el.Num }}</td>
<td>{{ el.Intitule }}</td>
<td width="30">
<div class="dropdown">
<button class="btn btn-primary
dropdown-toggle" type="button" data-toggle="dropdown">Action
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a id="{{ forloop.counter }}" class="dropdown-item"
onclick="beforeFunction(this);" href="#">Créer avant</a></li>
<li><a id="{{ forloop.counter }}" class="dropdown-item"
onclick="afterFunction(this);" href="#">Créer après</a></li>
<li><a class="dropdown-item" onclick="" href="#">Créer first child</a>
</li>
<li id="actionsep"><a class="dropdown-item" href="#"
id="actionsep">Créer last child</a></li>
<li><a class="dropdown-item" href="Modifier_PC/{{ el.id }}">Modifier</a>
</li>
<li><a class="dropdown-item" href="Supprimer_PC/{{ el.id
}}">Supprimer</a></li>
</ul></div></td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
<script>
function beforeFunction (element) {
var myTable = document.getElementById("myTable");
var currentRow = myTable.insertRow(element.id);
var compte = document.createElement("input");
compte.setAttribute("id", "idcompte");
compte.setAttribute("name", "compte");
compte.setAttribute("class", "form-control");
var intitule = document.createElement("input");
intitule.setAttribute("name", "intitule");
intitule.setAttribute("class", "form-control");
var button = document.createElement("button");
button.setAttribute("name", "action" + element.id);
button.setAttribute("class", "btn btn-primary dropdown-toggle");
button.setAttribute("data-toggle", "dropdown");
var textnode1 = document.createTextNode("Action");
var textnode2 = document.createTextNode("Enregistrer");
var textnode3 = document.createTextNode("Annuler");
var span = document.createElement("span");
span.setAttribute("class", "caret");
var ul = document.createElement("ul");
ul.setAttribute("class", "dropdown-menu");
var li1 = document.createElement("li");
var li2 = document.createElement("li");
var Enregistrer = document.createElement("a");
Enregistrer.setAttribute("href", "UpdatePC_before");
var Supprimer = document.createElement("a");
Supprimer.setAttribute("href", "{% url 'comptes' %}");
var div = document.createElement("div");
div.setAttribute("class", "dropdown");
var currentCell = currentRow.insertCell(-1);
currentCell.appendChild(compte);
currentCell = currentRow.insertCell(-1);
currentCell.appendChild(intitule);
Enregistrer.appendChild(textnode2);
Supprimer.appendChild(textnode3);
li1.appendChild(Enregistrer);
li2.appendChild(Supprimer);
ul.appendChild(li1);
ul.appendChild(li2);
button.appendChild(textnode1);
button.appendChild(span);
currentCell = currentRow.insertCell(-1);
div.setAttribute("class", "dropdown");
div.appendChild(button);
div.appendChild(ul);
currentCell.appendChild(div);
}
解决方案
好像您正在创建一个输入,但没有将其附加到表单中。
document.querySelector('form.formClassName').appendChild(compte)
推荐阅读
- java - 如何在 IDE 之外启动 JavaFX 11 应用程序?
- react-native - React Native - deepsyx 的家庭自动化,如何运行它?
- sql - 向 SQL Server 表添加特定索引以提高性能
- python - Django:不同数据库中的 save() 方法
- c# - .net Core 获取已启动应用程序的配置
- angular - 使用 === 评估数字 1 与单元类别规则中的数字 0 不同
- r - 在 bookdown 的等式参考中,\tag 不能与 (\#eq:label) 一起使用
- java - 双向链表方法的问题
- javascript - 如何在初始加载时在 NgbDatePicker 的输入字段中显示日期
- javascript - 用于检查单词中间多余空格(多个)的正则表达式