jQuery Validation plugin: disable validation for specified submit buttons

后端 未结 12 819
故里飘歌
故里飘歌 2020-11-22 16:14

I have a form with multiple fields that I\'m validating (some with methods added for custom validation) with Jörn Zaeffere\'s excellent jQuery Validation plugin. How do you

12条回答
  •  清歌不尽
    2020-11-22 16:39

    I found that the most flexible way is to do use JQuery's:

    event.preventDefault():
    

    E.g. if instead of submitting I want to redirect, I can do:

    $("#redirectButton").click(function( event ) {
        event.preventDefault();
        window.location.href='http://www.skip-submit.com';
    });
    

    or I can send the data to a different endpoint (e.g. if I want to change the action):

    $("#saveButton").click(function( event ) {
        event.preventDefault();
        var postData = $('#myForm').serialize();
        var jqxhr = $.post('http://www.another-end-point.com', postData ,function() {
        }).done(function() {
            alert("Data sent!");
        }).fail(function(jqXHR, textStatus, errorThrown) {
            alert("Ooops, we have an error");
        })
    

    Once you do 'event.preventDefault();' you bypass validation.

提交回复
热议问题