在某些情况下,我的代码不会刷新父页面

时间:2014-08-22 06:57:09

标签: javascript php wordpress-plugin

我正在使用一个厚盒子,并希望在thicbox关闭时刷新父页面。我使用下面的代码,但它有时会工作,在某些情况下不起作用...这是问题吗?

<script>
    function close2()
    {
       parent.tb_remove();
       parent.location.reload(1)
    }
</script>

<input type="submit" name="save_new_task" id="save_new_task" onclick="close2()" style="width: 100px;text-align: center;" 
  class="button button-primary button-medium" value="<?Php _e("register","creat_mysite");?>"/>

2 个答案:

答案 0 :(得分:0)

也许是因为您在JavaScript执行之前提交表单?如果您想提交表单然后执行Javascript功能,可以试试这个:

$(document).ready(function()
{
    $('#save_new_task').click(function(e)
    {
        e.preventDefault();

        //Serialize your form
        //POST request


        //Maybe also try window.opener instead of parent
        //window.opener refers to the window that called window.open( ... ) to open the window from which it's called
        //window.parent refers to the parent of a window in a <(i)frame>
        window.opener.tb_remove();     
        window.opener.location.reload(); 
    });
});          

答案 1 :(得分:0)

我不知道背景,但从我可以弄清楚尝试以下 试试这个:

   <script>
        function close2()
        {
           parent.tb_remove();
           parent.location.reload(1);
return true;
        }
    </script>
    <form onsubmit="return close2()" >
    <input type="submit" name="save_new_task" id="save_new_task" style="width: 100px;text-align: center;"  class="button button-primary button-medium" value="<?Php _e("register","creat_mysite");?>"/>
</form>
相关问题