javascript - jQuery 对话框 : how to prevent closing the dialog + an extra get request!

标签 javascript jquery jquery-ui

工作流程: 用户单击该按钮,将打开一个包含搜索表单的对话框。一个ajax post请求被发送到服务器,并得到一个json响应。我收到成功处理程序的回调。现在有两个问题。

  1. 对话框在成功回调 (successFn) 后关闭。我在成功回调中得到 json 响应,我希望用户看到结果并按关闭按钮终止对话框。

    1. 对话框关闭后不久,就会向服务器发送一个获取请求。自行关闭对话框后,url 为 http://localhost:8080/search?query= 。我没有明确发送任何 GET 请求

    jQuery(文档).ready(函数(){ jQuery("#myButton").click( showDialog );
            $myWindow = jQuery('#myDiv');
    
            $myWindow.dialog({ width: 400, autoOpen:false, title:'Hello World',
                    overlay: { opacity: 0.5, background: 'black'},  
                    modal: true,    
                    /*open: function (type, data) {
                        // include modal into form
                        $(this).parent().appendTo($("form:first"));
                    },      */          
                    buttons: {
                        "Submit Form": function() { $('form#myform').submit();},
                        "Cancel": function() {$(this).dialog("close");}
                            }
                    });
            });            
    
    
    var showDialog = function() {
        $myWindow.show(); 
        $myWindow.dialog("open");    
        }
    
    var closeDialog = function() {
        $myWindow.dialog("close");
    }
    
    var successFn = function (response) {   
            var obj = JSON.parse(response); 
            $("#result").html('').html(obj.name);
    }
    var errorFn =  function (xhr, ajaxOptions, thrownError){
                $("#myform").parent().html('').html(xhr.statusText);
                }
    var query = $("input#query").val(); 
    var dataString = 'query='+ query ;  
    
    $('form#myform').submit(function(){
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: '/search',
            async: false,
            data: $("#myform").serialize(),
            success: successFn,
            error: errorFn
        });
    });     
    

最佳答案

最好将 method="post" 添加到表单中,以避免意外发送 GET 数据。

添加return false;成功函数可能会阻止对话框关闭。如果可以的话我会测试一下。

编辑:还要检查所有代码是否都在 jQuery(document).ready( function(){

希望有帮助!

关于javascript - jQuery 对话框 : how to prevent closing the dialog + an extra get request!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5682607/

相关文章:

css - 方形渐变背景图片、 Angular 半径和透明度

javascript - 查询 :invalid regular expression

javascript - 如何在 jQuery Ui Slider 中添加自定义增量和减量计数器

javascript - 如何解决 React.js 中的此路由问题?

javascript - 在 vuex 中分离数据

javascript - 使用 JavaScript 事件再次触发 CSS 动画

javascript - 如何测试调用 AJAX 函数而不使用 jasmine 触发它的函数?

javascript - 如何使用 Twitter Bootstrap 在图像上获取 div

javascript 幻灯片 slick_accessible_slideshow

javascript - jQuery 可拖放问题