简体   繁体   English

警报消息不起作用后重定向

[英]redirect after alert message not working

this is a snippet of a js file> 这是一个js文件的片段>

$('#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;

}));

as you can see there is an alert message after deleting a user from a list. 正如您所看到的,从列表中删除用户后会出现一条警告消息。

I want to refresh the page after ok on alert message is pressed so i added the line> 我想在按下警告消息后刷新页面,所以我添加了行>

window.location.reload();

but it's not working, why is this? 但它不起作用,为什么会这样? how can i fix it? 我该怎么办呢?

I've been trying to use alternative to this like 我一直在尝试使用替代品

location.href = '....';

window.location = '/some/url';

but nothing seems to work! 但似乎没有任何作用!

this is in my admin.php, the code for deleting user from the database: 这是在我的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);
    }
}

In order to simulate redirect in your browser try to: 要在浏览器中模拟重定向,请尝试:

Javascript way: Javascript方式:

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

jQuery way: jQuery方式:

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

Try this and let me know it it works for you or not. 试试这个让我知道它对你有用。

EDIT: Inside ajax success. 编辑:阿贾克斯成功。 Try to close modal window and try to replace method. 尝试关闭模态窗口并尝试替换方法。

EDIT 2: Put this part of code inside of your document ready block and check is it fired or not if it is fired it means your form is reloading correctly. 编辑2:将这部分代码放在文档就绪块中并检查它是否被触发如果它被触发它意味着你的表单正在重新加载。

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

Elaborating on @charlietfl's comment, could you try something like this? 详细说明@ charlietfl的评论,你能尝试这样的事吗?

Return the count from the ajax script and insert it into your page: 从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;

}));

That would eliminate the need to refresh the page entirely and be a bit more friendly to use. 这将消除完全刷新页面的需要,并且使用起来更加友好。

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

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