【问题标题】:redirect after alert message not working警报消息不起作用后重定向
【发布时间】:2014-01-17 12:02:38
【问题描述】:

这是一个js文件的sn-p>

$('#btnYes').on('click', (function() {
var id = $('#myModal').data('id');
var usertype = $('#myModal').data('usert');

$.ajax({

  url: '{site_url()}admin/deleteUserFromList',
  type: 'POST',
  data: {id: id, userT: usertype},
  success: function(html){

    $('[data-id='+id+']').parents('tr').remove();
    $('#myModal').modal('hide');
    alert('usuario borrado');
    window.location.reload();
  }
});
 return false;

}));

如您所见,从列表中删除用户后会有一条警告消息。

我想在按下 ok on alert 消息后刷新页面,所以我添加了这一行>

window.location.reload();

但它不起作用,这是为什么呢?我该如何解决?

我一直在尝试使用类似的替代方法

location.href = '....';

window.location = '/some/url';

但似乎没有任何效果!

这是在我的admin.php中,从数据库中删除用户的代码:

public function deleteUserFromList(){

    if ((isset($_POST['id']) && (isset($_POST['userT'])))){
        $rowId = $_POST['id'];
        $userType = $_POST['userT'];

        $result = array();

        if($userType == 'front'){
            $front = UserManager::getInstance()->getUser($rowId);
            UserManager::getInstance()->deleteItem($front);

        }else{
            $back = UserBackManager::getInstance()->getUser($rowId);
            UserBackManager::getInstance()->deleteItem($back);
        }
        $result["message"] = "Usuario eliminado";

        echo json_encode($result);
    }
}

【问题讨论】:

  • 这似乎在 ubuntu 上的 Chrome 31 中运行良好...这可能是与浏览器相关的问题吗?
  • 换句话说:您使用的是什么浏览器?
  • here location.reload 在每个浏览器中实现的方法。
  • @antindexer:这正是他使用的功能。
  • @Lix 嗨,我在 Windows 8 上使用 Chrome 31。使用 chrome 工具检查后我没有错误

标签: javascript jquery


【解决方案1】:

为了在浏览器中模拟重定向,请尝试:

Javascript方式:

window.location.replace("http://stackoverflow.com");

jQuery方式:

var url = "http://stackoverflow.com";    
$(location).attr('href',url);

试试这个,让我知道它是否适合你。

编辑: 里面ajax成功。尝试关闭模态窗口并尝试替换方法。

编辑 2: 将这部分代码放在您的文档就绪块中,并检查它是否被触发,如果它被触发则意味着您的表单正在正确重新加载。

$( window ).unload(function() {
  alert( "Bye now!" );
});

【讨论】:

  • 感谢,但似乎没有任何效果,我正在发疯>s
  • 你知道如何在 Chrome 中调试吗?
【解决方案2】:

详细说明@charlietfl 的评论,你能尝试这样的事情吗?

从 ajax 脚本返回计数并将其插入到您的页面中:

$('#btnYes').on('click', (function() {
  var id = $('#myModal').data('id');
  var usertype = $('#myModal').data('usert');

  $.ajax({

    url: '{site_url()}admin/deleteUserFromList',   // this returns the user count as data
    type: 'POST',
    data: {id: id, userT: usertype},
    success: function(data){

      $('[data-id='+id+']').parents('tr').remove();
      $('#countDisplayElement').text(data);  // insert new count into current page
      $('#myModal').modal('hide');
      alert('usuario borrado');
      window.location.reload();
    }
  });
 return false;

}));

这将消除完全刷新页面的需要,并且使用起来更加友好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多