表单提交中的JQuery确认对话框中的问题

时间:2013-03-19 21:48:05

标签: asp.net-mvc-3 jquery-ui jquery

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

  1. 验证所有必填字段(使用validate.js和unobstrusive.js提交表单)
  2. 检查下拉列表的值,如果它是部分类型,即(冗余),则显示用户确认对话框。
  3. 如果用户按是确认,则关闭确认对话框并调用Ajax
  4. 但问题是当我通过单击确认对话框上的“是”按钮确认时,对话框关闭但执行不会失败。

    即,序列化表单数据并进行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;
        });
    

1 个答案:

答案 0 :(得分:0)

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

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

redunt = "Active"

其他js功能

$(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;
    });