我有这样的链接
<a href="delete.PHP?id=1" class="delete">Delete</a>
如果用户点击它.一个确认应弹出,然后只有当用户单击是,它应该转到实际的url.
我知道这可以防止默认行为
function show_confirm() { var r=confirm("Are you sure you want to delete?"); if (r==true) { **//what to do here?!!** } } $('.delete').click(function(event) { event.preventDefault(); show_confirm() });
解决方法
您可以在点击中完成所有操作:
$('.delete').click(function(event) { event.preventDefault(); var r=confirm("Are you sure you want to delete?"); if (r==true) { window.location = $(this).attr('href'); } });
或者你可以通过将点击的元素传递给函数来实现:
function show_confirm(obj){ var r=confirm("Are you sure you want to delete?"); if (r==true) window.location = obj.attr('href'); } $('.delete').click(function(event) { event.preventDefault(); show_confirm($(this)); });