jQuery Validation on click without submit

2019-08-04 08:40发布

I am using this amazing script for validating my form and for the normal functionality it works great, but I would like to validate the form on a onClick (a href) event instead on the submit (input).. is that possible?

Script: https://github.com/jzaefferer/jquery-validation

It must be something like this, right?

<a href="#" onClick="validate()" >

2条回答
成全新的幸福
2楼-- · 2019-08-04 09:13

The jQuery validation plugin hooks to the submit event of the form. Therefore all you need to do is make the click of that a element submit the form. Try this:

$('#myLink').click(function(e) {
    e.preventDefault();
    $('#myForm').submit(); // this will then call the validate() func
});
查看更多
叼着烟拽天下
3楼-- · 2019-08-04 09:35

You can call the valid() method on the form to see whether it is valid.

<a href="#" onClick="$('myformselecto').valid()" >

I would recommend adding the handler using jQuery rather than inlining it

查看更多
登录 后发表回答