带有表单验证插件的jQuery UI对话框

时间:2010-12-15 17:39:01

标签: javascript jquery jquery-ui jquery-validate jquery-ui-dialog

我目前正在使用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 个答案:

答案 0 :(得分:13)

我通过3个步骤解决了类似的问题:

  1. 将验证器附加到表单:

    $('#your-form-id').validate();
    
  2. 单击模态表单的“保存”按钮后,提交表单(将触发验证程序):

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

    $('#your-form-id').validate({
      submitHandler: function(form) {
        // do stuff
      }
    });
    

答案 1 :(得分:4)

验证器validate函数只是设置验证,它不会触发它。提交表单或写入字段时会自动完成触发。尝试将验证代码添加到open事件中:

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

答案 2 :(得分:3)

我知道这个问题已经过时了。但是当我在寻找这个特殊问题时,这个问题就出现了。所以我认为这可能有助于其他人。终于成功实现了这一目标。

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

答案 3 :(得分: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;
        }
 },

答案 4 :(得分:1)

我在jQuery Validator插件(http://jqueryui.com/dialog/)中使用jQuery Dialog插件(http://jqueryvalidation.org/)时遇到了同样的问题。问题是jQuery-UI对话框没有附加到表单,它附加在&lt; / body&gt;之前,因此要验证的元素在&lt; form&gt;&lt; / form&gt;之外。部分。

要解决此问题,请在Dialog初始化中添加“open”属性。在这个属性中,我添加了一个函数,它将我的Dialog div元素包装在一个表单元素中,然后初始化验证器。

另外,我在Dialog初始化时添加了“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>
相关问题