admin管理员组文章数量:1221308
I'm trying to figure out why this JavaScript doesn't stop the form from being submitted:
<form action="" id="form">
<input type="text" />
<input type="submit" />
</form>
<script>
var code = function () {
return false;
};
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
}
</script>
Unless I add the following onsubmit attribute to the form element:
<form action="" id="form" onsubmit="return false">
<input type="text" />
<input type="submit" />
</form>
<script>
var code = function () {
return false;
};
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
}
</script>
Seems like the addEventListener method alone should do the trick. Any thoughts? I'm on a Mac and I'm experiencing the same result on Safari, Firefox and Opera. Thanks.
I'm trying to figure out why this JavaScript doesn't stop the form from being submitted:
<form action="http://www.example.com" id="form">
<input type="text" />
<input type="submit" />
</form>
<script>
var code = function () {
return false;
};
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
}
</script>
Unless I add the following onsubmit attribute to the form element:
<form action="http://www.example.com" id="form" onsubmit="return false">
<input type="text" />
<input type="submit" />
</form>
<script>
var code = function () {
return false;
};
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
}
</script>
Seems like the addEventListener method alone should do the trick. Any thoughts? I'm on a Mac and I'm experiencing the same result on Safari, Firefox and Opera. Thanks.
Share Improve this question asked Jun 8, 2009 at 23:12 GR1000GR1000 1- possible duplicate of return false on addEventListener submit still submits the form? – unor Commented Sep 16, 2013 at 11:40
3 Answers
Reset to default 8Combined the information from the two very helpful answers into a solution that works on both Mac and PC:
<script>
var code = function (eventObject) {
if (eventObject.preventDefault) {
eventObject.preventDefault();
} else if (window.event) /* for ie */ {
window.event.returnValue = false;
}
return true;
};
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
} else if (element.attachEvent) {
element.attachEvent("onsubmit", code);
}
</script>
Looks like if you change your function to
var code = function(e) {
e.preventDefault();
}
It should do what you're looking for.
source
I think what you're looking for is the preventDefault()
method of the Event
interface for browsers that implement it. It will cancel the form submission in the way you expected "return false
" to.
More here and here.
<script type="text/javascript">
var code = function (evt) {
if (evt.preventDefault) {
evt.preventDefault();
}
return false;
};
window.onload = function() {
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
}
};
</script>
本文标签: javascriptForm is still submitted even though listener function returns falseStack Overflow
版权声明:本文标题:javascript - Form is still submitted even though listener function returns false - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739312956a2157668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论