簡體   English   中英

jQuery驗證成功后如何調用jquery post方法

[英]How to call jquery post method after jquery validation success

當我在驗證規則中調用submithandler時,我想調用post函數來提交表單,但它沒有觸發。

下面是我的代碼

$(function() {
    $.validator.addMethod("time", function(value, element) {
        return this.optional(element) || /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/i.test(value);
    }, "Please enter a valid time (24 Hour Format).");

    //^(([0-1]?[0-2])|([2][0-3])):([0-5]?[0-9])(a|p)m?$  12 hour
    //  /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/i 24 hour
    // alert($(#isNoOfExecutionSet").val());
    jQuery.validator.setDefaults({
        highlight: function(element) {
            jQuery(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            jQuery(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'label label-danger',
        errorPlacement: function(error, element) {
            if (element.parent('.input-group').length) {
                error.insertAfter(element.parent());
            } else {
                error.insertAfter(element);
            }
        }
    });
    $('#form').validate({

        rules: {
            full_name: {
                required: true,
                minlength: 3,
                maxlength: 100

                // email: true
            },
            short_name: {
                required: true,
                minlength: 3,
                maxlength: 100
                    // number: true
                    // email: true
            },
            password: {
                required: true,
                minlength: 3,
                maxlength: 100
                    // number: true
                    // email: true
            },
            total_branches: {
                required: true,
                number: true
                    // email: true
            },
            confirm: {
                equalTo: "#password"
            },
            submitHandler: function(form) {
                alert('jj');

            }
        }
    });
}); // end of function

下面是我的html代碼

<div class="container">
    <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
        <div class="modal-body">
            <div class="row">
                <div class="col-xs-12">
                    <h2> Institute Info</h2>

                    <form role="form" method="post" id="form">
                        <hr class="colorgraph">
                        <p style="color:#f0776c;float:left" id="msg"></p>

                        <div class="form-group">
                            <input type="text" class="form-control input-lg" id="full_name" name="full_name" type="full_name" value="saljdf" placeholder="Enter Full Name">
                        </div>
                        <div class="form-group">
                            <input type="tet" class="form-control input-lg" id="short_name" name="short_name" type="short_name" value="saljdf" placeholder="Enter Short Name">
                        </div>
                        <div class="form-group">
                            <input type="number" class="form-control input-lg" id="total_branches" name="total_branches" value="12" type="total_branches" placeholder="Enter Total Branches Allowed">
                        </div>
                        <div class="form-group">
                            <input type="password" class="form-control input-lg" id="password" name="password" type="password" value="123" placeholder="Enter Password">
                        </div>
                        <div class="form-group">
                            <input type="password" class="form-control input-lg" id="confirm" name="confirm" type="password" value="saljdf" placeholder="Confirm Password">
                        </div>
                        <div class="form-group">
                            <select class="form-control" id="status" name="status">
                                  <option value="Active" >Active</option>
                                  <option value="Non-Active" >Non-Active</option>
                              </select>
                        </div>
                        <hr class="colorgraph">
                        <div class="row">
                            <!--<div class="col-xs-12 col-md-6"><input type="submit" value="Register" class="btn btn-primary btn-block btn-lg" tabindex="7"></div>-->
                            <div class="col-xs-12 "><button type="submit" class="btn btn-lg btn-primary btn-block" name="btnsave" value="btnsave" id="btnsave">Create Institute</button></div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

您的意思是ajax調用,即jquery post方法。 您可以使用unun函數編寫ajax調用,並且只要表單校驗成功地從該循環/條件語句中調用相同的ajax函數。

喜歡。

     function myFunction(){
        // JQuery post method i.e Ajax call
     }

    $("form"). submit(function(){
       // validation cade
       if(validate){
          myFunction();
        }
    });

這不是確切的代碼,我只是嘗試建議在許多您可以嘗試的項目中對我有用的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM