jQuery UI对话框与表单验证插件

7
我目前正在使用bassistance validation plugin来验证我的表单。我使用弹出式模态对话框来容纳需要验证的表单,但由于某种原因它没有调用我的表单……所有我的ID和引用都在工作,但我仍然没有任何成功。
也许有人可以为我解惑。这是我的JavaScript代码。
$("#addEventDialog").dialog("open");

$("#addEventDialog").dialog({
    title: 'Add Event',
    modal: true,
    buttons: {
        "Save": function() {
            $("#interestForm").validate({
                submitHandler: function(form) {
                    $("#calendarWidget2").ajaxSubmit({
                        target: "#calendarResponse",
                        dataType: 'json',
                        beforeSubmit: function () {
                            $('input[type=submit]').attr("disabled", true);
                            $("#calendarResponse").show('slow');
                        },
                        success: function(response, event) {
                            if(response.status == true) {
                                $('input[type=submit]').attr("disabled", false);
                                $("#calendarResponse").delay(5000).fadeOut('slow');

                                //If the widget says it's okay to refresh, refresh otherwise, consider it done
                                if(response.refreshEvents == '1') {
                                    $("#calendar").fullCalendar("refetchEvents");
                                }
                                // Close the dialog box when it has saved successfully
                                $("#addEventDialog").dialog("destroy");
                                // Update the page with the reponse from the server
                                $("#calendarResponse").append("Successfully Added: "+ response.title +"<br />");
                            } else {
                                $("#calendarWidget2").validate();
                                $("#calendarResponse").append("ERROR: "+ response.status +"<br />");    
                            }
                        },
                        error: function() {
                            alert("Oops... Looks like we're having some difficulties.");    
                        }
                    });
                }
            });
        },
        "Cancel": function() { 
            $(this).dialog("close"); 
        } 
    }
});
5个回答

14

我用以下3个步骤解决了类似的问题:

  1. 将验证器绑定到表单上:

    $('#your-form-id').validate();
    
  2. 当您的模态表单的保存按钮被点击时,提交表单(验证器将被触发):

  3. buttons: {
      "Save": function() {
        $('#your-form-id').submit();
      },
    
    将提交逻辑移动到验证器 submitHandler 中:
    $('#your-form-id').validate({
      submitHandler: function(form) {
        // do stuff
      }
    });
    

4

验证器的validate函数仅设置验证,不触发它。当表单提交或字段被写入时,自动触发验证。尝试将您的验证代码添加到open事件中:

$("#addEventDialog").dialog("open");
            $("#addEventDialog").dialog({
                open: function() {
                    $("#interestForm").validate({
                        ...
                    });
                }, ...

不错! 另一个选项是使用jQuery的on()方法来监听对话框的dialogopen事件: $("<parent to addEventDialog>").on("dialogopen", function(event, ui) { $("#interestForm").validate();}); - eh1160

3

我知道这个问题很久远了,但当我搜索这个特定的问题时,它是最先出现的。所以我想这可能有助于其他人。最终成功地解决了这个问题。

请参见http://jsfiddle.net/536fm/6/


1
尝试像这样做。将表单验证内容放在对话框脚本之外,或者我猜开启回调也可以。
 buttons : {
       "Cancel" : function() {
            $(this).dialog('close');
        },
        "Submit" : function() {
            var isValid = $("#yourForm").valid();
            if(isValid) {
                var formData = $("#yourForm")serialize();
                // pass formData to an ajax call to submit form.

            }
           return false;
        }
 },

1
我使用jQuery对话框插件(http://jqueryui.com/dialog/)和jQuery验证插件(http://jqueryvalidation.org/)时遇到了相同的问题。问题是jQuery-UI对话框没有附加到表单中,而是附加在</body>之前,因此要验证的元素位于<form></form>部分之外。
为解决此问题,我在对话框初始化中添加了“open”属性。在此属性内,我添加了一个函数,将我的对话框div元素包装在一个表单元素内,然后初始化验证器。
同时,我在对话框初始化中添加了“close”属性。在此属性内,我添加了一个函数,取消包装在打开事件上的表单,并重置验证器。
以下是一个简单的例子:
<script type="text/javascript">
var validator;
$(document).ready(function () {
    $("#dialog-id").dialog({
    autoOpen: false,
    resizable: true,
    width: 450,
    modal: true,
    // Open event => wraps the Dialog source and validate the form.
    open: function (type, data) {
        $(this).wrap("<form id=\"form-id\" action=\"./\"></form>");
        validator = $("#form-id").validate();
    },
    // Close event => unwraps the Dialog source and reset the form to be ready for the next open!
    close: function (type, data) {
        validator.resetForm();
        $(this).unwrap();
    },
    buttons: {
        "Cancel": function () {
            $(this).dialog("close");
        },
        "Create": function () {
            validator.form();
        }
    }
});
</script>

一些与上述JavaScript相关的HTML代码:
<div id="dialog-id" title="Thematic Section">
    <div class="form_description">
        Create a thematic section for a conference type.
    </div>
    <ul style="list-style-type:none;">
        <li>
            <label class="description" for="conferencetype_id">Conference Type:</label>
            <div>
                <select id="conferencetype_id" name="conferencetype_id" style="width:150px;"> 
                    <option value="1" selected="selected">Type-1</option> 
                    <option value="2" selected="selected">Type-2</option>
                    </select>
            </div> 
        </li>
        <li>
            <label class="description" for="title">Title:</label>
            <div>
                <input id="title" name="title" type="text" maxlength="100" value="" style="width:350px;" required/> 
            </div> 
        </li>
    </ul>
</div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接