html - 多个表单生成的jquery不提交
问题描述
我正在用 Django 建立一个网站(类似电子商务)。
在某些时候,我会显示一个项目列表,每个项目都有一个带有提交按钮订单和数量选择器的表单。
我已经实现了过滤器功能,它删除我的项目列表的 html 代码并用 jquery 匹配的项目重建它。
单击提交按钮时,此函数生成的新表单不执行任何操作
这是我在函数中使用的部分代码(我使用 ajax 调用生成匹配项的 json,然后将它们显示在表列表中):
$.each(code_json, function(index, value){
var string = "<tr id="+ value.material +"><td>"+ value.manufNo +"</td><form method='POST' action='/add_to_cart/"+value.material+"/"+ value.node+"/{{language}}'><td><input type='submit' class='btn' value='Commander' style='color:blue;'></td><td><input id='qty' type='number' name='qty' class='form-control' value='1'></td></form></tr>";
$("#header").after(string);
});
我知道通常使用 Django 你必须为每个表单添加{% csrf_token %}。但是,它通常在丢失时会抛出错误页面,并且这里不显示任何内容
编辑:我试图在动态创建的提交按钮上绑定一个 onclick 事件。在此我在 jquery 中做了一个 $.post 来模拟表单的提交,但没有发生任何事情
$(document).on('click', '.btnStandard', function(event) {
console.log($(this).attr('id'));
$.post('/add_to_cart/'+$(this).attr('id'),
{
qty: $("#qty"+$(this).attr('id')).val()
},function(data,status,xhr){
alert("Data : "+data+", status: "+status+", xhr: "+xhr);
});
它在控制台中打印 $(this).attr('id') 但它没有做任何其他事情 谢谢你的帮助
解决方案
它没有解释为什么我有这个问题,但我找到了解决我的问题的解决方法。
我不是动态生成表单,而是使用模板生成它们,然后将它们全部隐藏。后来,多亏了 css,我让我需要的东西在我需要的时候可见。
推荐阅读
- binary - 二进制左移和右移
- rest - 休息 api 有效负载字段的正确日期格式是什么?
- android - google play:如何将不同的 apk 分发到不同的国家但版本相同?
- c# - 由于 postsharp 错误,构建失败:管道服务器提前退出,退出代码为 1
- android - 如何使用观察者使用 ViewPager 中当前项目位置的值验证 EditText 中的值?
- regex - 使用 PCRE 从代码中删除 htmlentities(.*)
- docker - Gitlab - 获取作业时发生错误
- c# - 在 Google Cloud Datastore 上使用动态类型
- c - 在 Windows 中,代码能够获取有关内部电池的信息,但不能获取外部电池的信息
- django - Django:如何将常见的表单提交部分组织成一个函数?