最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - `onsubmit` of html form doesn't work - Stack Overflow

programmeradmin3浏览0评论

The html code is simple:

<form onsubmit="submitAdvice(this);return false;">
    <input type="submit" value="submit" />
</form>​

And the javascript code:

function submitAdvice(f) {
    alert('submitting');
}

But when I click the submit button, it doesn't alert submitting, instead, it submits the form.

I don't know where is wrong.

PS: the live demo: /


I just found the reason, it caused by jsfiddle!

Please note it will load mootools 1.4.5 on onLoad event. Everything will be OK if I set it as no wrap

The html code is simple:

<form onsubmit="submitAdvice(this);return false;">
    <input type="submit" value="submit" />
</form>​

And the javascript code:

function submitAdvice(f) {
    alert('submitting');
}

But when I click the submit button, it doesn't alert submitting, instead, it submits the form.

I don't know where is wrong.

PS: the live demo: http://jsfiddle/yc6cq/


I just found the reason, it caused by jsfiddle!

Please note it will load mootools 1.4.5 on onLoad event. Everything will be OK if I set it as no wrap

Share Improve this question edited Dec 13, 2012 at 13:05 Freewind asked Dec 13, 2012 at 12:57 FreewindFreewind 198k163 gold badges452 silver badges734 bronze badges 4
  • 2 have you considered registering the event handler properly using addEventListener()? Would be much cleaner! – ThiefMaster Commented Dec 13, 2012 at 12:59
  • 1 I just found the problem is caused by jsfiddle. It uses mootools by default(on the left panel). Everything is well when I removed it. – Freewind Commented Dec 13, 2012 at 13:02
  • You just create file in your local system and try – Miqdad Ali Commented Dec 13, 2012 at 13:07
  • Possible duplicate of HTML form action and onsubmit issues – Suma Commented Sep 27, 2016 at 12:12
Add a ment  | 

2 Answers 2

Reset to default 4
<form onsubmit="return submitAdvice(this);">
    <input type="submit" value="submit" />
</form>​

function submitAdvice(f) {
    alert('submitting');
    return false;
}

Try with this form statement

<form onsubmit=" return submitAdvice(this);">

http://jsfiddle/T3Qae/

发布评论

评论列表(0)

  1. 暂无评论