jquery验证:在整个表单验证后调用ajax并关闭bootstrap模式表单

时间:2013-03-30 21:58:14

标签: jquery ajax twitter-bootstrap jquery-validate

我正在使用Bootstrap布局和the jQuery Validation Plugin。我点击一个按钮,打开表格的模态。用户输入一些数据并对其进行改进,直到一切正确。当他提交它时,它应该调用ajax并关闭模态形式(但如果验证失败,则应该没有ajax并且没有模态关闭)。以下是我的代码:

ajax call

$('#outcomeFormDialog form').on( "submit", function( event ) {
    var form = $(this);
    $.ajax({
        type: form.attr('method'),
        url: "../php/client/json.php",
        data: form.serialize(),
        success: function(data, status) {
            $(this).modal('hide');
        }
    });

    event.preventDefault();
});

当前验证码:

 $('#outcomeFormDialog form').validate(
 {
  rules: {
    amount: {
      money: true,
      required: true
    },
    comment: {
      required: false
    }
  },
  highlight: function(element) {
    $(element).closest('.control-group')
    .removeClass('success').addClass('error');
  },
  success: function(element) {
    element
    .addClass('valid').closest('.control-group')
    .removeClass('error').addClass('success');
  }
 });

据我所知,jquery验证的success对应于每个表单元素而不是整个表单 - 因此检查整个表单验证应该以其他方式完成。

这是我的表格(小胡子驱动):

<div id="outcomeFormDialog" class="modal hide fade" role="dialog">
    <form class="form-horizontal well" data-async data-target="#outcomeFormDialog" method="POST">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3 id="myModalLabel">Add outcome</h3>
        </div>
        <div class="modal-body">
            <fieldset>
                <input name="type" type="hidden" value="add_outcome" />
                <div class="control-group">
                    <label class="control-label" for="amount">Amount</label>
                    <div class="controls">
                        <div class="input-prepend">
                            <span class="add-on">{{ currency }}</span>
                            <input type="text" name="amount" class="input-xlarge" placeholder="00.01" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="comment">Comment</label>
                    <div class="controls">
                        <input type="text" name="comment" class="input-xlarge" placeholder="Comment here..." />
                    </div>
                </div>
            </fieldset>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Save outcome</button>
        </div>
    </form>
</div>

1 个答案:

答案 0 :(得分:9)

你对这个插件有几个流行的问题和误解:

  • 您不需要submit处理程序。此插件会自动捕获并处理click按钮的submit事件。

  • As per the docs,您应该将ajax置于插件的submitHandler回调选项中。

  

submitHandler :回调,默认:默认(本机)表单提交

回复处理表单时的实际提交   已验证。获取表单作为唯一参数。替换默认值   提交。 通过Ajax提交表单的正确位置   验证

假设您的ajax写得正确,请将您的代码重新安排为更像这样的内容:

$(document).ready(function () {

    $('#outcomeFormDialog form').validate({  // initialize plugin
        rules: {
            amount: {
                // money: true, //<-- no such rule
                required: true
            },
            comment: {
                required: false // superfluous; "false" same as leaving this rule out.
            }
        },
        highlight: function (element) {
            $(element).closest('.control-group')
                .removeClass('success').addClass('error');
        },
        success: function (element) {
            element.addClass('valid').closest('.control-group')
                .removeClass('error').addClass('success');
        },
        submitHandler: function (form) {
            // form validates so do the ajax
            $.ajax({
                type: $(form).attr('method'),
                url: "../php/client/json.php",
                data: $(form).serialize(),
                success: function (data, status) {
                    // ajax done
                    // do whatever & close the modal
                    $(this).modal('hide');
                }
            });
            return false; // ajax used, block the normal submit
        }
    });

});

工作演示:http://jsfiddle.net/duAkn/