0

在一个 JQuery 对话框中,我有四个字段。当我单击保存按钮时,我需要检查并验证以下内容

  1. 验证所有必填字段(在使用 validate.js 和 unobtrusive.js 提交表单时)
  2. 检查下拉列表的值,如果它属于特定类型,即(冗余),则向用户显示确认对话框。
  3. 如果用户按 Yes 确认,则关闭确认对话框并调用 Ajax

但问题是当我通过单击确认对话框上的“是”按钮进行确认时,对话框关闭但执行没有停止。

即,序列化表单数据并进行 Ajax 调用以调用 Web 服务。

请任何人都可以帮忙。

$(function () {

        $('form').submit(function () {

            $('#result').html(" ");

            var redunt = null;
            redunt = $(ClientCrud_StatusCodeId).find('option:selected').text();

            if ($(ClientCrud_StatusCodeId).find('option:selected').text() == "Redundant") {
                $('#clientRedundantMessage2').html("Client once made redundant cannot be reactivated. Are you sure ?");
                $("#RedundantMessage2").dialog(
                    {
                        autoOpen: false,
                        height: 170,
                        width: 420,
                        modal: true,
                        resizable: false,
                        title: "Confirmation for Redundant",
                        Content: "Fields cannot be left blank.",
                        buttons: {
                            "Yes": function () {
                                redunt = "Active";
                                $('#RedundantMessage2').dialog('close');
                            },
                            "No": function () {
                                $(this).dialog("close");
                                return false;
                            }
                        }
                    }) //.dialog("widget").draggable("option", "containment", "none");
                $("#RedundantMessage2").dialog("open");
            }

            if ($(this).valid()) 
            {
                debugger;
                if (redunt == "Active") {

                    $.ajax({
                        url: this.action,
                        type: this.method,
                        async: false,
                        cache: false,
                        data: $(this).serialize(),
                        error: function (request) {
                            $("#result").html(request.responseText);
                            //    event.preventDefault();
                        },
                        success: function (result) {
                            if (result == "success") {
                                $.ajax({
                                    url: "/Client/ClientGrid",
                                    type: 'POST',
                                    data: { "page": 0 },
                                    datatype: 'json',
                                    success: function (data) {
                                        $('#grid').html(data);
                                    },
                                    error: function () {
                                        alert('Server error');
                                    }
                                });

                                $('#myEditClientDialogContainer').dialog('close');
                                $('#myEditClientDialogContainer').remove()
                            }
                            else {
                                clearValidationSummary();
                                var a = '<ul><li>' + result + '</li></ul>';
                                $('#result').html(a);

                            }
                        }
                    });
                }
            }

            $("#griderrormsg1 li").hide().filter(':lt(1)').show();
            return false;
        });

        editallowed = true;
    });
4

1 回答 1

0

我认为您的代码序列有问题,当函数$("#RedundantMessage2").dialog( ...... );执行时,在这种情况下不等待用户响应“是”或“否”所以......你的标志redunt = "Active"没有意义。

按钮选项具有在选择选项时执行的功能,因此您必须调用一个函数来执行帖子

$(function () {

        $('form').submit(function () {

            $('#result').html(" ");

            var redunt = null;
            redunt = $(ClientCrud_StatusCodeId).find('option:selected').text();

            if ($(ClientCrud_StatusCodeId).find('option:selected').text() == "Redundant") {
                $('#clientRedundantMessage2').html("Client once made redundant cannot be reactivated. Are you sure ?");
                $("#RedundantMessage2").dialog(
                    {
                        autoOpen: false,
                        height: 170,
                        width: 420,
                        modal: true,
                        resizable: false,
                        title: "Confirmation for Redundant",
                        Content: "Fields cannot be left blank.",
                        buttons: {
                            "Yes": function () {
                                redunt = "Active";
                                trySend();
                                $('#RedundantMessage2').dialog('close');
                            },
                            "No": function () {
                                $(this).dialog("close");
                                return false;
                            }
                        }
                    }) //.dialog("widget").draggable("option", "containment", "none");
                $("#RedundantMessage2").dialog("open");
            }



            $("#griderrormsg1 li").hide().filter(':lt(1)').show();
            return false;
        });

        editallowed = true;
    });

另一个js函数

function trySend(){

    if ($('#IdOfYourForm').valid()) 
        {
            debugger;
            if (redunt == "Active") {

                $.ajax({
                    url: this.action,
                    type: this.method,
                    async: false,
                    cache: false,
                    data: $(this).serialize(),
                    error: function (request) {
                        $("#result").html(request.responseText);
                        //    event.preventDefault();
                    },
                    success: function (result) {
                        if (result == "success") {
                            $.ajax({
                                url: "/Client/ClientGrid",
                                type: 'POST',
                                data: { "page": 0 },
                                datatype: 'json',
                                success: function (data) {
                                    $('#grid').html(data);
                                },
                                error: function () {
                                    alert('Server error');
                                }
                            });

                            $('#myEditClientDialogContainer').dialog('close');
                            $('#myEditClientDialogContainer').remove()
                        }
                        else {
                            clearValidationSummary();
                            var a = '<ul><li>' + result + '</li></ul>';
                            $('#result').html(a);

                        }
                    }
                });
            }
        }

}
于 2013-03-19T22:09:12.000 回答