我有一个引导工具提示,我想从 AJAX 请求中加载数据,请求中的文本是title
工具提示的属性。我的 AJAX 请求工作正常,但我有两个问题:
- 为什么来自 AJAX 调用的数据没有进入工具提示?
- 如何使用我的
ttManager
对象来封装工具提示的状态?
目前,当页面首次加载并在控制台中单击时#btnSubmit
,我看到
success
了来自 console.log(ttManager) 行的正确数据
$(document).ready(function () {
//this object's title attribute will be the value of ttManager.title seen below
var ttManager = {
title: '',
setTitle: function (data) {
this.title = data;
}
}
var ajaxCall = function () {
//this returns the top five results of text from a query
$.ajax({
type: "POST",
contentType: "application/json",
url: "Service.asmx/GetDrugs",
dataType: "json",
success: function (data) {
console.log('success');
ttManager.title = data.d;
//inside this function I want to set ttManager.title equal to the data.d
console.log(ttManager);
},
error: function (xhr) {
console.log('failed: ' + xhr.status);
}
});
}
$('#btnSubmit').tooltip({
//reference to ajax call
//title is the attribute responsible for displaying text in the tooltip
//I need to use a reusable object to set the text property instead of referencing ajaxCall
//would it be better if there below were title: ttManager.title?
title: ajaxCall,
trigger: 'click',
placement: 'right'
});
});
我很确定我在某个地方需要一个回调函数,但我不确定在哪里。任何未来的指针也将不胜感激。谢谢。