Jquery验证多次调用submitHandler

时间:2016-08-31 04:57:45

标签: javascript jquery validation unobtrusive-validation

我使用unobtrusive-validation来验证我的jQuery表单。提交表单时,我需要在发布表单之前调用我的自定义函数。在这里,我使用了提交处理程序,满足了我的需求。

但功能代码多次调用。

请从下面找到我的代码;

    <!DOCTYPE html>
    <html>
    <head>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>
        <script>
        $(function() {

      var validator = $("#myForm").data('validator');    
      validator.settings.submitHandler = function (form) {
      var target = $(form).attr('onvalidated');
       window[target]();
           if (typeof window[target] === 'function') {
                formok = window[target]();
                form.submit();
                }
            };
    });

    function MyFunction()
    {
    alert('called');
    }

  </script>
    <style>
        .field-validation-error {
            color: red;
        }

        .input-validation-error {
        border: 1px solid red !important;
      }
    </style>
    </head>
    <body>

    <form action="action_page.php" id="myForm" onvalidated="MyFunction">
      First name:<br>
      <span class="field-validation-valid" data-valmsg-for="fname" data-valmsg-replace="true"></span>
    <input type="text" id="fname" name="fname" data-val-required="First name is required" data-val="true" />

      <br>
      Last name:<br>
        <span class="field-validation-valid" data-valmsg-for="lname" data-valmsg-replace="true"></span>
    <input type="text" id="lname" name="lname" data-val-required="Last name is required" data-val="true" />

      <br><br>
      <input type="submit" value="Submit">
    </form>
    </body>
    </html>

这里警报得到2次。请帮忙解决。

我需要在所有验证通过后或在发布之前调用一次。

0 个答案:

没有答案