简体   繁体   English

提交表格后如何延迟弹出窗口的关闭?

[英]How to delay the close of popup window after submitting the form?

I use popup window to fill out form and submit data to MySQL. 我使用弹出窗口填写表格并将数据提交给MySQL。 If I manually close the the popup window after submitting the data, it works fine. 如果我在提交数据后手动关闭弹出窗口,则可以正常工作。 However, if I close the window by using "onsubmit" to trigger "window.close", the window is immediately closed, but many times the data is not saved. 但是,如果我通过使用“ onsubmit”触发“ window.close”来关闭窗口,则该窗口会立即关闭,但是很多时候数据不会保存。 I guess the window closed too fast, even before the data has been sent to the server. 我想即使在数据发送到服务器之前,窗口也关闭得太快。 I tried to delay the close of the window by using setTimeout function. 我试图通过使用setTimeout函数来延迟关闭窗口。 I cannot make it to work, I mean the delay of closing of popup window. 我无法使其正常工作,是指关闭弹出窗口的延迟。

This is the parent window code: 这是父窗口代码:

<input type="submit" value="my phsical template" onclick="popupUploadForm()"/> 

<script type="text/javascript"> 
function popupUploadForm(){
    var newWindow = window.open('physical.php');
} 
</script>

The popup window code. 弹出窗口代码。 Window is closed after pushing "save". 按下“保存”后,窗口关闭。 But most of the time, data is not saved. 但是大多数情况下,不会保存数据。

  <form  method="post" action="physical.php" onsubmit="closeSelf()">
  <textarea name="text" id="textarea" cols="45" rows="10"></textarea>
  <div><input type="submit" value="Save"/></div>

<script type="text/javascript">
 function closeSelf(){
     self.close();
  };   
 </script>

Try to delay the close of the window. 尝试延迟关闭窗口。 it doesn't work. 它不起作用。

 function closeSelf(){
 setTimeout(function(){
self.close();
},1000);  }; 

Simply disable the submit button, wait for the server response and then close your modal using promises: http://jsfiddle.net/5L52o0Le/ 只需禁用提交按钮,等待服务器响应,然后使用Promise关闭您的模式: http : //jsfiddle.net/5L52o0Le/

<div id="modal">
    <form id="myform" class="ui form">
        <div class="field">
            <label>Username</label>
            <input type="text"/>
        </div>
        <div class="field">
            <label>Password</label>
            <input type="password"/>
        </div>
        <button class="ui primary button">Save</button>
    </form>
</div>

javascript JavaScript的

$('#myform').submit(function(e) {
    // Prevent normal submit to server, use JavaScript instead
    e.preventDefault(); 

    // Check if save already in progress
    if ($('#myform .primary').hasClass('disabled')) {
        return;
    }

    $('#myform .primary').addClass('disabled');

    // Send form data to server...
    var promise = $.ajax({
        url: 'https://api.github.com/repos/laravel/laravel/issues?state=closed',
        method: 'GET'
    });

    // Ran after success
    promise.done(function() {
        console.log('closing modal... TODO');
    });

    promise.fail(function() {
        console.log('failed... something went wrong');
    });

    // Regardless of fail or success
    promise.always(function() {
        $('#myform .primary').removeClass('disabled');
    });
});

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

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