python - 模板中没有来自 jquery 的值到我的视图函数
问题描述
我正在使用 django,我从复选框中获取所有 id,现在我想将它们传递给我的视图函数,但我不知道如何。我试过 request.GET.getlist('vals') 但没有成功。有什么建议么?
事件.html:
<script>
$(document).ready(function(){
$("button").click(function(){
type: 'POST';
var vals = [];
$.each($("input[name='checkb']:checked"),function(vals){
vals.push($(this).attr('id'));
});
alert("values: " + vals.join(", "));
});
});
</script>
<td><a href="eprint"><button><i class="bi bi-sim"></i></button></a></th>
{% for l in object_list %}
<tr>
<td>
<form>
<label><input type="checkbox" id={{l.pk}} name="checkb"></label>
<form>
...
网址.py:
path('eprint',views.eprint,name='eprint'),
视图.py:
def eprint(request):
print('eprint')
v=request.GET.getlist(vals)
解决方案
好的,我终于解决了这个问题。我正在使用 AJAX 将 id 发送到我的视图:
模板中的java脚本:
<script>
$(document).ready(function() {
$("button").click(function() {
var vals = [];
$.each($("input[name='checkb']:checked"), function() {
vals.push($(this).attr('id'));
});
console.log('cons2:',vals)
$.get('eprint/ticked/',{marked: vals})
console.log('after')
});
});
</script>
视图.py:
def eprint(request):
print('eprint')
print(request.GET)
推荐阅读
- c# - 使用回车 CR(“\r”)和换行 LF(“\n”)作为分隔符的字符串(数组)连接和拆分操作上的 UWP C# 令人费解的行为
- opengl - OpenGL SSAO 奇怪的神器
- javascript - 在反应中获取数据的最佳方法是什么?
- c - 用巴比伦方法计算 $\sqrt[3]{x}$
- javascript - 将一个数组拆分为相等和的相等连续子数组
- html - 使用 REGEX 以 html 形式验证欧洲逗号分隔的价格
- vba - TransferSpreadsheet - 运行时错误 4298 - 指定范围
- python - 使用onclick按钮登录网站提交
- r - R使用变量访问列表元素
- android - 如何过滤房间中的行