我有一个表单,我正试图阻止用户使用javascript进行双重提交。由于某些原因,即使处理程序函数返回false并且调用了event.preventDefault,表单仍然会多次提交。有人知道为什么吗?另外,我如何让它按预期的方式工作?
document.getElementById('my_form').addEventListener("submit", function(evt){
if ( validationFunction(document.getElementById('submit_button')) == false) {
evt.preventDefault();
return false;
}
});函数validationFunction(){ return false }
<form method="post" id="my_form">
<input name='textbox'>
<input type="submit" id="submit_button">
</form>发布于 2016-06-30 07:00:46
用一种简单的方式试试。像这样
HTML
<form id="my_form">
<input name='textbox'>
<input type="button" id="submit_btn">
</form>JS
document.getElementById("submit_btn").addEventListener("click", function(){
document.getElementById("my_form").submit(); // if validation is true run this
});发布于 2016-06-30 07:13:19
document.getElementById('my_form').addEventListener("submit", function(evt){
if ( validationFunction(document.getElementById('submit_button')) == false) {
// anything here
evt.preventDefault();
return false;
}
// anything here
event.preventDefault();
});https://stackoverflow.com/questions/38111139
复制相似问题