Javascript&Django-Confirm()cancel不应遵循href [英] Javascript & Django - Confirm() cancel should not follow href

查看:60
本文介绍了Javascript&Django-Confirm()cancel不应遵循href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Django,为了防止从datatable选项中删除项目,我在 confirm 函数中遇到了一些问题.

I'm using Django in my project and I'm getting some problems with the confirm function in order to prevent item deletion from datatable option.

尽管在确认弹出窗口中取消了该链接,但该链接仍将我发送到默认链接href,并且该对象已删除.

Despite cancelling in the confirm pop-up, the link still sends me to the default link href and the object is deleted.

这是我的HTML:

<a class='btn btn-danger btn-xs' id="elimina-objeto" onclick="confirmaEliminacion()" href="{% url 'eliminarQueja' queja.id %}">Eliminar</a>

还有我的JS:

function confirmaEliminacion() {
  var res = confirm("Va a eliminar el objeto seleccionado. Si desea continuar, pulse aceptar.");

  if (res) {
    return false;
  } else {
    document.getElementById("elimina-objeto").href = "#";
  }
}

推荐答案

对HTML进行一些小的更改:

Make a small change to your HTML:

<a class='btn btn-danger btn-xs' id="elimina-objeto" onclick="return confirmaEliminacion()" href="{% url 'eliminarQueja' queja.id %}">Eliminar</a>

此外,如果用户要删除并遵循原始的href,则应该从javascript函数返回 true ,如果用户不想删除,则应返回 false .无需更改href值本身:

Also, you should be returning true from your javascript function if the user wants to delete and follow the original href, and false if they do not want to delete. Changing the href value itself is not necessary:

function confirmaEliminacion() {
  var res = confirm("Va a eliminar el objeto seleccionado. Si desea continuar, pulse aceptar.");
    if (res) {
        return true;
    } else {
        return false;
    }
}

这篇关于Javascript&amp;Django-Confirm()cancel不应遵循href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆