简体   繁体   English

jQuery对话框:如何防止关闭对话框+额外的获取请求!

[英]jQuery dialog: how to prevent closing the dialog + an extra get request!

The work flow: user click the button, a dialog box opens with a search form. 工作流程:用户单击按钮,将打开一个带有搜索表单的对话框。 An ajax post request is sent to the server, and get a json response. ajax post请求被发送到服务器,并获得json响应。 I get the callback on success handler. 我得到了成功处理程序的回调。 Now two issues. 现在有两个问题。

  1. the dialog closes upon success callback (successFn). 成功回调(successFn)后对话框关闭。 I get the json response in the success call back, and I want the user to see the result and press close button to terminate the dialog,. 我在成功回调中得到了json响应,我希望用户看到结果并按关闭按钮终止对话框。

    1. Soon after the dialog closes, a get request is sent to server. 对话框关闭后不久,将向服务器发送get请求。 After closing the dialog by itself, the url is like http://localhost:8080/search?query= . 关闭对话框后,url就像http:// localhost:8080 / search?query = I do not send any GET request explicitly 我没有明确发送任何GET请求

    jQuery(document).ready( function(){ jQuery("#myButton").click( showDialog ); jQuery(document).ready(function(){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 }); }); 

It would be a good idea to add method="post" to your form to avoid any accidental GET data being sent. 最好在表单中添加method="post"以避免发送任何意外的GET数据。

Adding return false; 添加返回false; to the success function may stop the dialog from closing. 成功功能可能会停止对话框的关闭。 I'll test this if I can. 如果可以,我会测试一下。

Edit: also check that all your code is inside jQuery(document).ready( function(){ 编辑:还检查所有代码是否在jQuery(document).ready( function(){

Hope that helps! 希望有所帮助!

我想你只需要添加return false

"Submit Form": function() { $('form#myform').submit(); return false;},

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

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