[英]function called by onsubmit never reached
Probably a daft oversight but I have an HTML form similar to this: 可能是一个愚蠢的疏忽,但我有一个类似于这样的HTML表单:
<form onsubmit="updateProfile();">
<input type="submit" value="Update Account">
..
...
</form>
And: 和:
function updateProfile() {
// never gets here
};
I set a breakpoint in the updateProfile()
function but when I click the Update Account
button it never gets there. 我在updateProfile()
函数中设置了一个断点,但是当我单击“ Update Account
按钮时,它永远不会到达那里。
Can anyone give me a likely explanation? 谁能给我一个可能的解释?
A form without an action attribute is not a form, according to standards - and will actually cause a page reload in some browsers. 根据标准,没有action属性的表单不是表单 - 实际上会在某些浏览器中导致页面重新加载。
To avoid this behavior update from 要避免此行为更新
<form onsubmit="updateProfile();">
to 至
<form onsubmit="updateProfile(); return false;">
<form id="myForm">
<input type="submit">
</form>
then in javascript you can do this 然后在JavaScript中你可以做到这一点
$('#myForm').on('submit', function(event){
event.preventDefault();
updateProfile();
});
and don't forget to import jquery library :) 并且不要忘记导入jquery库:)
It seems i cannot reproduce your issue, look here is it similar? 看来我无法重现你的问题,看这里是否相似?
codepen
http://codepen.io/icrosil/pen/bdXyPL codepen
http://codepen.io/icrosil/pen/bdXyPL
<form onsubmit="updateProfile(event);">
<input type="submit" value="Update Account">
..
...
</form>
function updateProfile(e) {
e.preventDefault();
alert("it gets here buddy");
};
可能是单击处理程序在文档中的某个位置处于活动状态,捕获单击事件,阻止发生默认操作(返回false
或使用event.preventDefault()
),因此submit
事件永远不会从表单中触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.