javascript - Ignoring onSubmit when the submit button is clicked - Stack Overflow

I have a form where I've specified onSubmit="validate()", but I want to ignore the valid

I have a form where I've specified onSubmit="validate()", but I want to ignore the validation if the submit-button was clicked. Is there a good cross-browser way of detecting if the submit button was clicked and thus ignoring the validation?

I have a form where I've specified onSubmit="validate()", but I want to ignore the validation if the submit-button was clicked. Is there a good cross-browser way of detecting if the submit button was clicked and thus ignoring the validation?

Share Improve this question asked Mar 9, 2010 at 14:32 KCLKCL 6,88310 gold badges38 silver badges43 bronze badges 1
  • first, never do "onThis = that()". do it the unobtrusive way. What exactly do you want? If you want to ignore validation, remove it from onSubmit and validate only when a form element was changed! – N 1.1 Commented Mar 9, 2010 at 14:50
Add a ment  | 

5 Answers 5

Reset to default 3

Why don't you use a button instead of a submit, and set it's action on the click of the button? That way you can control if you want to validate, submit, or whatever else you like.

The submit event only fires if the form is submitted by the user; not if it is submitted via JS.

Therefore:

<input type="submit" onclick="this.form.submit(); return false;">

If JS is not available, this acts like a normal submit button … and the onsubmit still fails to fire as it also requires JS.

(Attaching events using JS instead of intrinsic event attributes is, as usual, preferred by excluded from this example for the sake of clarity)

you can try to use a <input type="button"... with an onClick that submits the form - a javascript .submit() doesn't fire the onSubmit-function of the form.

Did you try this?

<input type="submit" onclick="void(window.validate=function(){return true;})" value="Submit" />

Just return false, or preventDefault from your submit button handler

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742413386a4439259.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信