简体   繁体   English

Django表单向导无法阻止使用Javascript提交表单

[英]Django form wizard cannot prevent form submission with Javascript

I am using a form wizard in django and I want to allow the form to submit only if a Javascript condition is true. 我正在django中使用表单向导,并且仅在Javascript条件为true时才允许表单提交。

For starters, let's say that I always want to block the form submission. 对于初学者来说,我一直想阻止表单提交。 So, I am trying something like this: 所以,我正在尝试这样的事情:

$('form[name=mainform]').submit(function(event) {
  alert('Submit blocked!');
  event.preventDefault();
  return false;
});

However, the form is always submitted normally and moves to the next step of the wizard ! 但是,该表单通常是正常提交​​的,并移至向导的下一步!

I tried everything (setting the onClick of the submit button and return false) but nothing worked. 我尝试了所有操作(设置了提交按钮的onClick并返回false),但没有任何效果。 I have used similar techniques and they were working in normal forms (not django). 我使用过类似的技术,并且它们以正常形式(而不是django)工作。 Can anybody explain to me what's the problem ? 有人可以向我解释什么问题吗?

I have to mention that for my own reason I don't want to do it through Django (by inserting a hidden field and setting its value through javascript and then checking it during the clean form). 我不得不提到,由于我自己的原因,我不想通过Django(通过插入隐藏字段并通过javascript设置其值,然后在干净的表单中对其进行检查)来​​完成此操作。

Thanks ! 谢谢 !

将侦听器添加到表单的“提交”按钮,而不是表单本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM