简体   繁体   English

JS Close表单提交窗口

[英]JS Close window on form submission

Update at bottom of post: 在帖子底部更新:

I have a page where when a button is pressed, a new window will popup with a form. 我有一个页面,当按下按钮时,将弹出一个带有窗体的新窗口。 Below is where I open the popup. 下面是我打开弹出窗口的地方。

<a><span class="add-on"><i class="icon-plus" style="color: green;" onclick="window.open('<?php echo $this->Html->Url(array('controller' => 'customers', 'action' => 'add?popup')); ?>', 'Add Customer', 'height=630, width=430')"></i></span></a>

When the form is submitted, I need to close that window. 提交表单后,我需要关闭该窗口。 The problem I am having now is that the window is closing before the form is fully getting submitted. 我现在遇到的问题是,在完全提交表单之前,该窗口已关闭。

Popup Window Code: 弹出窗口代码:

<fieldset style="padding-left: 15px;">
<legend>Add Customer</legend>

<?php echo $this->Form->create('Customer', array('inputDefaults' => array('div' => false, 'label' => false))); ?>
    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
            <?php echo $this->Form->input('first_name', array('placeholder' => 'First Name')); ?>
        </div>
    </div>

    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
            <?php echo $this->Form->input('last_name', array('placeholder' => 'Last Name')); ?>
        </div>
    </div>

    <div class="input-prepend">
        <span class="add-on"><i class="icon-globe"></i></span>
        <?php echo $this->Form->input('location', array('placeholder' => 'Location')); ?>
    </div>

    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-phone"></i></span>
            <?php echo $this->Form->input('phone_number', array('class' => 'maskedPhone', 'placeholder' => 'Phone Number')); ?>
        </div>
    </div>

    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-envelope"></i></span>
            <?php echo $this->Form->input('email', array('placeholder' => 'Email')); ?>
        </div>
    </div>

    <!-- Close Form -->
<?php echo $this->Form->end(array(
    'label' => 'Save Customer',
    'id' => 'saveCust',
    'class' => 'btn btn-primary',
    'div' => false
)); ?>

<?php echo $this->Html->script('jquery.maskedinput.min', array('inline' => false));
  echo $this->Html->script('maskedinput', array('inline' => false));

$this->start('script'); ?>
    <script type="text/javascript">
        $(document).ready(function () {     
            inputs.maskedInputs();

            // Get queryString to close popup window
            var queryString = window.location.search.substring(1);
            if (queryString == "popup"){
                $('#CustomerAddForm').submit(function() {
                    window.close();                 
                });                 
            }
        });
    </script>
<?php $this->end(); ?>

Trying to use Ajax call now with no luck: 现在尝试使用Ajax调用没有运气:

$('#saveCust').click(function() {
                    alert("test");
                    $.ajax({
                        type: "POST",
                        url: "<?php echo $this->Html->Url(array('controller' => 'Customers', 'action' => 'add')); ?>",
                        data: $('#CustomerAddForm').serialize(),
                        success: function(data)
                        {
                            alert("Test");
                            window.close();
                        }
                    })
                });
                return false;               

You have a race condition and the window.close will always win. 您有比赛条件,window.close将始终获胜。 You need to close when the form is DONE submitting. 表单提交完成后,您需要关闭。

Either convert it over to use an Ajax call or call the window.close() in the response of the form submission. 可以将其转换为使用Ajax调用,也可以在表单提交的响应中调用window.close()。

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

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