我想用AJAX调用的Web服务填充popover(从引导程序)。
我试过了:
$.ajax({
type: "POST",
url: "myURL?action=fetchData&ID=123",
success: function(msg) {
$('.key_cal').popover({
title:"User Profile",
placement:'right',
content:"<h5>Name: "+msg[0].firstName+"</h5>",
html: true
});
}
});
但我需要在popover中调用AJAX来填充内容,但我对此一无所知;你能帮帮我吗?
现在,我不想在我的url(AJAX)中用123硬编码ID,而是想使用为popover单击的元素的ID。(我已将每个元素的id作为密钥传递)
非常感谢。
$('.key_cal').on('click', function (e) {
e.preventDefault();
var id = this.id;
var $this = $(this);
$.ajax({
type: "POST",
url: "myURL?action=fetchData&ID=" + id
}).done(function (msg) {
$this.popover({
title: 'myTitle',
content: function () {
return "<h5>Name: " + msg[0].firstName + "</h5>";
},
html: true
}).popover('show');
});
});
希望这能有所帮助,但我不确定你为什么要通过URL(查询字符串)发送POST
请求的数据?相反,您可以将其指定为ajax调用的data
对象。
您需要为每个键指定cal或使用popover的任何类,而不是在AJAX上启动popover。
$(document).ready() {
$(".key-cal").hover(setPopover);
}
function setPopover() {
var myTitle = "";
var myContent = "";
var myHtml = true;
$.ajax({
// Go grab your data
data: 'id=' + $(this).prop('id'),
success: //Set your variables
});
$(this).popover({
title: myTitle,
content: myContent,
html: true
}).popover('show');
}, function () {
$(this).popover('hide');
});
}
这不是最好或最有效的方法,但它对你来说应该很有意义,让你从那里开始。在悬停时启动popover ajax调用,设置数据,然后显示popover。