html - 复选框即使选中也不会提交
问题描述
正如您在此处看到的,我有用于修改管理员权限的复选框的代码。但是,即使选中复选框,它们也不会发送到服务器。我不明白为什么会这样。单击提交按钮时,仅发送隐藏的输入和 csrf_token。
<tbody>
{% for profile in admins %}
<tr>
<td>{{ profile.player.guid }}</td>
<td>{{ profile.user.username }}</td>
<form method="post">
{% with permissions=profile.admin_permissions_as_list %}
{% csrf_token %}
<input type="hidden" name="action" value="modify,{{ profile.user }}">
<td style="text-align: center"><input class="form-check-input" type="checkbox" value="ban" {% if permissions.0 == '1' %}checked="checked"{% endif %}></td>
<td style="text-align: center"><input class="form-check-input" type="checkbox" value="unban" {% if permissions.1 == '1' %}checked="checked"{% endif %}></td>
<td style="text-align: center"><input class="form-check-input" type="checkbox" value="checkplayers" {% if permissions.2 == '1' %}checked="checked"{% endif %}></td>
<td style="text-align: center"><input class="form-check-input" type="checkbox" value="viewlog" {% if permissions.3 == '1' %}checked="checked"{% endif %}></td>
<td style="text-align: center"><input class="form-check-input" type="checkbox" value="key" {% if permissions.4 == '1' %}checked="checked"{% endif %}></td>
<td style="text-align: center"><input class="form-check-input" type="checkbox" value="faction" {% if permissions.5 == '1' %}checked="checked"{% endif %}></td>
<td><button type="submit" class="btn btn-sm btn-outline-primary">修改权限</button></td>
{% endwith %}
</form>
<td>
<form method="post">
{% csrf_token %}
<input type="hidden" name="action" value="remove,{{ profile.user }}">
<button type="submit" onclick="return confirmRemove('{{ profile.user.username }}');" class="btn btn-sm btn-outline-danger">取消权限</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
解决方案
你有两个主要问题。
- 表单不能是 a 的父元素
<td>
(或 a 的子元素<tr>
) - 如果控件没有名称,则控件无法发送其
name=value
数据(并且您的复选框都缺少名称)
推荐阅读
- node.js - 在前端应用程序中隐藏 api 密钥
- hololens - 使用全息远程播放器应用程序,我总是只看到一个带有“正在接收...”文本的黑屏
- redis - Redis 数组同步问题(lpop、rpush)
- c# - 有人可以帮我吗?我无法获得正确的输出,我尝试多次更改公式,但仍然无法
- struct - 我收到“已读取 ScrollView contentOffset 绑定”警告
- android - Android-Jetpack-Compose @Composable 注解错误
- javascript - 更改 div 显示时提交的表单
- javascript - React Material UI 分页
- mongodb - 嘿伙计们,关于压缩选项的一些问题
- facebook-graph-api - 让用户通过 Instagram 传记验证他们的帐户