jquery验证插件ajax提交无法正常工作

时间:2015-04-25 04:40:22

标签: javascript jquery html ajax twitter-bootstrap

我使用jquery验证插件以bootstrap模式形式进行验证,当我发送表单时,jquery验证插件不起作用,表单也无法发送。

bootstrap模式表单

 <div class="modal fade" id="form-content" tabindex="-1" role="dialog"
             aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close"
                                data-dismiss="modal" aria-hidden="true">
                            &times;
                        </button>
                        <h3>send message</h3>
                    </div>
                    <div class="modal-body">
                        <form class="contact">
                            <fieldset>
                                <div class="modal-body">
                                    <ul class="nav nav-list">
                                        <div class="form-group">
                                            <label for="topic" class="control-label ">topic:</label>
                                            <input class="form-control"  type="text" name="topic">
                                            <span class="help-block"></span>
                                        </div>
                                        <div class="form-group">
                                            <label for="ruser" class="control-label ">ruser: </label>
                                            <input class="form-control"  type="text" name="ruser">
                                            <span class="help-block"></span>
                                        </div>
                                        <div class="form-group">
                                            <label for="content" class="control-label ">content:</label>
                                            <textarea class="form-control" name="content" rows="3"></textarea>
                                            <span class="help-block"></span>
                                        </div>
                                    </ul>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-success" type="submit">send</button>
                        <a href="#" class="btn" data-dismiss="modal">close</a>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>


                 <button class="btn btn-success btn-lg" data-toggle="modal"
                                    data-target="#form-content">
                                send message
                            </button>

的javascript

$(document).ready(function(){
    $('form').validate({
        rules: {
            topic: {
                required: true
            },
            ruser: {
                required: true
            },
            content: {
                required: true
            }
        },

        messages : {
            topic: {
                required: 'enter topic'
            },
            ruser: {
               required: 'enter ruser'
            },
            content: {
                required: 'enter content'
            }
        },
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
            if(element.parent('.input-group').length) {
                error.insertAfter(element.parent());
            } else {
                error.insertAfter(element);
            }
        }

        submitHandler: function (form) {
            alert('valid form submission'); // for demo
            $.ajax({
                type: "POST",
                url: "process.php",
                data: $('form.contact').serialize(),
                success: function(msg){
                    if(msg=='no'){
                        alert(msg);
                    }
                    else if(msg=='ok!'){
                        alert(msg);
                        location.reload()
                    }
                },
                error: function(){
                    alert("failure");
                }
            });
            return false;
        }
    });
});

我该如何解决这个问题?

jsfiddle

2 个答案:

答案 0 :(得分:0)

问题是提交按钮不是您尝试提交的表单的后代。

一种可能的解决方案是为按钮编写一个单击处理程序,并从那里调用表单提交,如

        <div class="modal-footer">
            <button class="btn btn-success" id="submitcontact">send</button>
            <a href="#" class="btn" data-dismiss="modal">close</a>
        </div>

然后

$('#submitcontact').click(function(){
    $('form.contact').submit();
})

演示:Fiddle

答案 1 :(得分:0)

首先,由于您的Jquery代码有一个错误:

在线号码40: - }替换为},
解决方案是在按钮的onclick事件上调用函数。无需移除type="submit",因为它已在Arun的答案中删除。

$(document).ready(function()内写下以下函数:

$('#sendForm').click(function(){
    $('form.contact').submit();
})

它会起作用。 WOKRING DEMO