event.preventDefault() does not work in Chrome and Opera

后端 未结 1 706
萌比男神i
萌比男神i 2021-01-20 15:37

I am using jQuery Validation Plugin 1.9.0 with jQuery 1.7.2.

I want my form to be validated with jquery validation plugin. It works fine in Firefox 20.0.1 and IE 10.

1条回答
  •  没有蜡笔的小新
    2021-01-20 16:02

    You are misunderstanding the purpose of .preventDefault(). It's not meant for preventing the default behavior of a function. It's meant for canceling the built-in behaviors of regular elements, i.e., remove the navigation and history from an anchor tag, etc.

    See: http://api.jquery.com/event.preventDefault/

    AFAIK, you cannot programatically turn validation on and off. Once you initialize the plugin with .validate(), there is no method to "un"-initialize it.

    However, to simply use a "cancel" button, there are several methods... one is to make sure you keep the cancel button outside of the

    and then use a click handler to do something else with it.

    DEMO: http://jsfiddle.net/TZVA6/1

    Or another method, as pointed out by Rocket Hazmat, is to give the button a type="button" attribute which prevents the default submit action.

    DEMO: http://jsfiddle.net/TZVA6/2/

    0 讨论(0)
提交回复
热议问题