javascript - 从工具提示中的按钮进行 Ajax 调用

标签 javascript jquery ajax tooltip

我一直在尝试从插入工具提示中的按钮进行 ajax 调用。我尝试使用 bootstrap tooltip 和 tooltipster,但似乎没有任何效果,而且我找不到有效的示例。请帮忙

这是我一直在尝试使用 Bootstrap 工具提示编写的更简单的代码版本。一旦工具提示出现在屏幕上,我就无法让 JQuery 识别按钮上的点击

//=====================HTML and PHP===========================
//previously made a query to get the image source

<label class="label" data-toggle="tooltip">
<?php
if($row['prof_image'] != null && $row['prof_image'] != ''){
echo'<img class="rounded-circle img-thumbnail" id="avatar" src="'.htmlentities($row['prof_image']).'" alt="profile"/>';
}else{
echo'<img class="rounded-circle img-thumbnail" id="avatar" src="../uploads/profile/profile.png" alt="profile" style="max-width:70%;"/>';
}
</label>

//=================Javascript===========================

$('#avatar').tooltip({title: "<p>Update image or <button class='btn btn-sm btn-outline-secondary' id='imgDel'>Delete</button></p>", html: true, placement: "bottom", delay: {show: 0, hide: 2000}}); //the tooltip has a button. When tootlip on screen I want to be able to press the button and make ajax call to delete image


$('#imgDel').click(function(){
alert('delete option!');  //I put an alert instead of ajax call to see if it works. It doesn’t
});


在这个阶段,我试图在按下工具提示中嵌套的按钮后收到警报,但我什至无法让它工作。请帮忙

最佳答案

只需使用 $(document).on('click', '#imgDel', function(){}) 而不是 $('#imgDel').click(function (){}) 因为当您初始化点击函数时,带有 #imgDel 的元素不存在。

$(document).on('click', '#imgDel', function(){
   alert('delete option!');
});

关于javascript - 从工具提示中的按钮进行 Ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717699/

相关文章:

javascript - 与循环相比,使用函数有什么优点?

javascript - 使用 POST 方法设置跨源 Cookie

jquery - MVC 中的下拉列表框可以触发一个事件,以便我可以填充另一个事件吗?

jquery - 在 header 中设置接受语言

jquery - Ajax:使用选择框将页面内容加载到div

javascript - xmlHttpRequest 不关闭连接

javascript - 为什么相同的 JS 代码不适用于第二个帖子/图像,但适用于第一个帖子/图像?

javascript - 通过多个元素的单个引用来 react : How to replace . querySelectorAll

javascript - 没有 jquery ui 的脚本

jquery - 在 Symfony 2 中接收 json 响应