简体   繁体   中英

function called by onsubmit never reached

Probably a daft oversight but I have an HTML form similar to this:

<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.

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.

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

$('#myForm').on('submit', function(event){
    event.preventDefault();
    updateProfile();
});

and don't forget to import jquery library :)

It seems i cannot reproduce your issue, look here is it similar?

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事件永远不会从表单中触发。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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