【发布时间】:2018-07-26 11:28:52
【问题描述】:
great answer here 介绍了如何在表单上使用 addEventListener 拦截 SUBMIT。 只要通过提交按钮(或 ENTER)提交表单,它就可以很好地工作。
虽然像这样被解雇,但它完全被忽略了:
document.getElementById('myform').submit();
你会如何拦截这样的电话?
示例如下:
<script>
function checkRegistration(){
var form_valid = (document.getElementById('some_input').value == 'google');
if(!form_valid){
alert('Given data is incorrect');
return false;
}
return true;
}
</script>
<form onsubmit="return checkRegistration()" method="get" action="http://google.com" id='myform'>
Write google to go to google..<br/>
<input type="text" id="some_input" value=""/>
<input type="submit" value="google it"/>
</form>
<a href="javascript:document.getElementById('myform').submit();">Ignore validation</a>
【问题讨论】:
-
我可能明显遗漏了stg,但在this example onsubmit 中被忽略了
-
好的。我明白了,你说得对,这有点奇怪。我会尽快发布一种可能的解决方案。
标签: javascript submit