【发布时间】:2021-08-17 17:28:11
【问题描述】:
这是我的第一个 Django 项目,我有这个简单的表单,我想提交所有行值(选中或未选中),我面临的错误是它只发送 row{num} 值,如果它被选中。
<script>
function calc(elemId)
{
var elem = document.getElementsByName(elemId)[0];
if (elem.checked)
{
elem.value = 1;
} else {
elem.value = 0;
}
}
</script>
<div class="container pt-3">
<form action="." method="post">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Keyword</th>
<th scope="col">seller_items</th>
<th scope="col">Total</th>
<th scope="col">checked</th>
</tr>
</thead>
{% csrf_token %}
{{ form|crispy }}
{%for i in data%}
<tr >
<th scope="row">{{forloop.counter}}</th>
<td>{{i.keyword}}</td>
<td>{{i.seller_items}}</td>
<td>{{i.total_found}}</td>
<td>
{% if i.checked %}
<input type="checkbox" name="row{{i.id}}" value="1" checked="checked" onclick="calc('row{{i.id}}')"/>
{%else%}
<input type="checkbox" name="row{{i.id}}" value="0" onclick="calc('row{{i.id}}')"/>
{%endif%}
</td>
</tr>
{% endfor %}
</table>
<div class="row">
<div class="col-12">
<button class="btn btn-primary float-right" type="submit">Update</button>
</div>
</div>
</form>
按下按钮时提交表单,但发布请求中只有选中的复选框行
【问题讨论】:
标签: javascript django