简体   繁体   English

jQuery UI对话框取消针对Ajax请求

[英]jquery ui dialog cancel for ajax request

Im trying to send multiple pieces of information via a form submit. 我试图通过表单提交发送多条信息。 On this submit I would like to return false (cancel page request as it has been ajax) and close the form. 关于此提交,我想返回false(取消页面请求,因为它已经是ajax)并关闭表单。 Unfortunately close seems to also do return, true i assume. 不幸的是,我认为,收盘似乎也确实会返回。 So that the post request doesn't fail. 这样发布请求不会失败。 This leaves the dialog still on the screen. 这使对话框仍然显示在屏幕上。 And if I call $("#dialog-form").dialog("close"); 如果我调用$(“#dialog-form”)。dialog(“ close”); then return false does not run and the page changes. 然后返回false不会运行,并且页面会更改。

If anyone knows how to fix this or if im doing something wrong it would be very helpful: 如果有人知道如何解决此问题,或者我做错了什么,那将非常有帮助:

javascript: javascript:

$('#modifyConsoleForm').submit(function(evt) {
    $.ajax({
        type : 'POST',
        url : jQuery("#modifyConsoleForm").attr("action"),
        data : jQuery(this).serialize(),
        dataType : "json",
        success : function(data) {
            hideError();
        },
        error : function(data) {
            setError('modify of console failed');
        }
    });
    $("#dialog-form").dialog("close");
    return false;
});

and my dialog initialisation: 和我的对话框初始化:

$("#dialog-form").dialog({
    autoOpen : false,
    height : 300,
    width : 350,
    modal : true,

    close : function() {
        allFields.val("").removeClass("ui-state-error");
    }
});

I have also tried always returning false in the dialogs close but it didnt seem to work. 我也尝试过在对话框关闭时始终返回false,但似乎没有用。 Am I missing something? 我想念什么吗?

Try using event.preventDefault() to stop the page changing 尝试使用event.preventDefault()停止页面更改

eg: 例如:

$('#modifyConsoleForm').submit(function(evt) {

    evt.preventDefault();

    $.ajax({
        type : 'POST',
        url : jQuery("#modifyConsoleForm").attr("action"),
        data : jQuery(this).serialize(),
        dataType : "json",
        success : function(data) {
            hideError();
        },
        error : function(data) {
            setError('modify of console failed');
        }
    });
    $("#dialog-form").dialog("close");
    return false;
});

Closing the dialog and returning true/false should be done when the ajax request has been finished, in the success and error functions. 在成功和错误功能中,应该在ajax请求完成后关闭对话框并返回true / false。

It is also wise that you put an activity indicator while you run ajax requests and hide/remove it once you got the response from the server, this way users know that there is a process going on. 明智的做法是,在运行ajax请求时放置一个活动指示器,并在从服务器获得响应后隐藏/删除该指示器,这样用户就知道正在进行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM