简体   繁体   English

关闭表单提交时关闭打开的弹出窗口,然后单击父窗口

[英]Close opened pop-up window on form submission and trigger click on the parent window

I have a parent window from where I am opening a pop-up window using window.open() and I am monitoring the return of that pop-up window using the window.opener method. 我有一个父窗口,我在那里使用window.open()打开一个弹出窗口,我正在使用window.opener方法监视该弹出窗口的返回。 I have two queries here: 我在这里有两个问题:

  1. On my pop-up window, there is a submit button that takes care of the submit functionality of the form and I would like to wait for the submit to complete and close this pop-up window on submission. 在我的弹出窗口中,有一个提交按钮,用于处理表单的提交功能,我想等待提交完成并在提交时关闭此弹出窗口。
  2. When the form is submitted and the pop-up window is closed, I am fetching one of the text box values and using that in my parent window to simulate a search automatically as soon as the pop-up window is closed. 提交表单并关闭弹出窗口时,我将获取其中一个文本框值,并在弹出窗口关闭后使用父窗口中的值自动模拟搜索。

Code: 码:

// parent window handling of information returned from the pop-up window
function HandlePopupResult(value) {
    alert("The field value is: " + value);
    $("input[value='Search']").trigger("click");
}

//pop-up window handling of information to be returned to the parent window on submission
$("#myForm").submit(function() {
    window.opener.HandlePopupResult($("#field").val());
    window.close();
});

So my question is: How can I make sure that the submit is completed in order for me to trigger a search click in my parent window and close this pop-up window? 所以我的问题是:如何确保提交已完成,以便我在父窗口中触发搜索点击并关闭此弹出窗口? Kindly let me know. 请告诉我。

I am able to finally send information back to my parent form properly but I just need to make sure that my submit gets completed. 我能够最终将信息正确地发送回我的父表单,但我只需要确保我的提交完成。 Any suggestions on making sure that I fire my events only after the submit is successful? 有关确保仅在提交成功后才启动我的活动的任何建议?

Cheers. 干杯。

The new window (popup) you have opened will load a new HTML page on submit (the function of the jQuery submit call is executed before the form is sent to the sever). 您打开的新窗口(弹出窗口)将在提交时加载新的HTML页面( 将表单发送到服务器之前执行jQuery submit调用的功能)。

You shall do the actions within the new page being loaded in the popup window. 您应该在弹出窗口中加载的新页面中执行操作。

This would be helpful for others in the future. 这将有助于将来的其他人。 I was able to achieve this myself by just modifying the above code to the code given below: 我能够通过将上面的代码修改为下面给出的代码来实现这一点:

// parent window handling of information returned from the pop-up window
function HandlePopupResult(value) {
    alert("The field value is: " + value);
    $("input[value='Search']").trigger("click");
}

Changed the form submission to an AJAX post call to ensure that the form submission has occurred and then carry out my remaining tasks of Handling the result of the pop-up window after which I close the popup window. 将表单提交更改为AJAX帖子调用以确保表单提交已发生,然后执行我处理弹出窗口结果的剩余任务,之后关闭弹出窗口。 Works like a charm! 奇迹般有效!

//pop-up window handling of information to be returned to the parent window on submission
$("#myForm").submit(function() {
        $.ajax({
          type: 'POST',
          url: $(this).attr('action'),
          data: $(this).serialize(),
          success: function(data) {
            window.opener.HandlePopupResult($("#field").val());
            window.close();
          }
        });
        return false;
      });

Another way to simplify that some would be using the $.post method instead. 另一种简化方法是使用$.post方法代替。 Saves a few lines, but achieves the same thing. 保存几行,但实现相同的目标。

$.post( url, data, callback); $ .post(url,data,callback);

$.post( $( "#myForm" ).attr('action'), $( "#myForm" ).serialize(), function(){
        window.opener.HandlePopupResult($("#field").val());
        window.close();
});

You can either throw that inside of your .submit as a shorthand replacement for the AJAX request, or throw it into its own function that you call from your form. 您可以将.submit作为AJAX请求的简写替换,或者将其放入您从表单调用的函数中。

<form onsubmit="submitHandler(this); return false" name="myForm" id="myForm">

You can also combine it with JSON to add some further functionality/validation before you have it sending things back to your main page, which could be helpful in many situations. 您还可以将它与JSON结合使用,以便在将内容发送回主页之前添加一些进一步的功能/验证,这在许多情况下可能会有所帮助。 As well, it's an awesome way of returning data from your PHP (or whatever you use) scripts back to your JS/jQuery. 同样,这是一种将PHP(或任何你使用的)脚本中的数据返回给JS / jQuery的绝佳方式。

$.post( $( "#myForm" ).attr('action'), $( "#myForm" ).serialize(), function(data){
      if(data.status == 'failed')
      {
        // Lets present an error, or whatever we want
      }
      else
      {
       // Success! Continue as we were
        window.opener.HandlePopupResult($("#field").val());
        window.close();
      }
}, 'json');

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

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