Fancybox 成功函数 close() 在 ajax 中不起作用

Fancybox success function close() is not working in ajax

index.php

 jQuery(function ($) {
      $("a.edit").fancybox();
 });


<a class="edit" href="showForm.php?id=<?=$row['id']?>"> Edit </a>

showForm.php

$id = $_GET['id'];

jQuery(function ($) {
$('#btnSend').click(function(){

        var key = $('input:hidden[name=id]').val();

        $.ajax({
                url: "process.php", 
                type: "POST",
                data: { "something": "something", "id": key}, 
                success: function(response) {
                    // I'm sure do something can work //

                    $.fancybox.close();  //this not work!
                    // parent.$.fancybox.close(); // or this
                }
        }); 
    });
});


<body>  
   *edit something...*

   <input type="hidden" name="id" value="<?=$id?>">
   <input type="button" id="btnSend" value=" Submit ">
</body>

process.php

$id = $_POST['id']; 
$something = $_POST['something'];

$sql = "UPDATE db 
        SET 
        something           =   '{$something}',
        WHERE id = '{$id}' ";
$result = $mysql->query($sql);  

我可以更新我的数据,但是
$.fancybox.close();
jQuery.fancybox.close();
parent.$.fancybox.close();
都不能用...

版本:fancybox-1.3.4

PS:当我使用 Ajax 在 samepage(index.php) 中提交表单时, $.fancybox.close(); 可以工作!

我已经发布了 jquery.fancybox-1.3.4.js: https://gist.github.com/vladkras/008361d790668634db74 的解压版本来弄清楚发生了什么。

当您的 close() 函数触发时,它似乎停在第 926 行:

if (busy || wrap.is(':hidden')) {

根据您的错误,wrap = $('<div id="fancybox-wrap"></div>') 未定义。尝试将 type: "iframe" 放入您的 fancybox 选项:

$("a.edit").fancybox({
    type: "iframe"
});

现在让它有 parent

UPD另外,你确定你需要那个中间文件showForm.php吗?原来你提出了两个ajax请求

index.php ----------> showForm.php ------------> process.php
           fancybox                     ajax

我认为你可以修改整个过程:

  1. 使用 ajax
  2. 从数据库接收元素
  3. 动态创建带有值的表单
  4. 用 fancybox 展示你的表格
  5. 发回更改的数据
  6. $.fancybox.close() 成功