不要在代码中显示确认消息有什么问题?

时间:2018-04-30 06:20:12

标签: javascript laravel

js功能:

$("#delCat").click(function(){
    //alert("test");
    if(confirm('Are you sure want to delete this category ?')){
        return true;
    } else {
        return false
    }
});

删除div:

<a id="delCat" href="delete-category/{{ $category->id }}" class="btn btn-danger btn-mini">Delete</a></td>

2 个答案:

答案 0 :(得分:0)

禁用链接的默认行为:

$("#delCat").click(function(e){
  e.preventDefault();
  //alert("test");
  if(confirm('Are you sure want to delete this category ?')){
    return true;
  }else{
    return false
  }
});

或直接附加到链接:

<a id="delCat" href="delete-category/{{ $category->id }}" class="btn btn-danger btn-mini" onclick="return confirm('Are you sure?');">Delete</a>

答案 1 :(得分:0)

$('#delCat').click(function(e){
  e.preventDefault();
  if(confirm('Are you sure ... ?')){
    return true; 
  } 
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<a id="delCat" href='link_to_delete'  class="btn btn-danger">delete</a>

我对jquery知之甚少,但我想,你应该防止事件目标的默认行为。