【发布时间】:2023-03-22 07:51:01
【问题描述】:
我正在使用 typeahead.js 来动态显示项目编号列表。当用户选择一个项目时,我有一个 Ajax 函数来获取项目余额并显示它们。这一切都很好。 我的问题是当我刷新页面并重新填充表单时。选定的项目 id 已填充,但我不知道如何再次触发 Ajax 脚本。
这是我最初用来在用户选择项目时触发代码的代码:$('input.typeahead').on('typeahead:selected', function (event, selection) {
typeaheadSelected(selection, event);
});
这是调用的ajax函数:
function typeaheadSelected($prsy, event) {
$('#project-distribution-loading').show();
//set the id of the selected bloc
if (event != null) {
var id = event.target.id;
$id = id.slice(-1);
} else {
$id = 0;
}
if (!$.isNumeric($id)) {
$id = 0;
}
$.ajax({
url: "../Helper/GetProjectDetails",
data: ({ prsy: $prsy }),
type: "GET",
success: function (data) {
$('#pi-details-panel').show(500);
data = JSON.parse(data);
if ($id == 0) {
$('#project-id-details').html($prsy);
}
else {
$('#project-id-details-' + $id).html($prsy);
}
//Convert amounts to currencies
$('#currency-transformation').val(data.FundAvailableAmount).currency();
$fundAmt = $('#currency-transformation').val();
if ($id == 0) {
$('#project-availability-details').html($fundAmt);
} else {
$('#project-availability-details-' + $id).html($fundAmt);
}
$('#currency-transformation').val(data.PendingRFApprovalOTPSAmount).currency();
$pendAmt = $('#currency-transformation').val();
$('#currency-transformation').val(data.NetAvailableAmount).currency();
$netAmt = $('#currency-transformation').val();
//Populate popover content
if ($id == 0) {
$popoverContent = "<span>Fund Amount</span><span class='right' id='popover-funding'>" + $fundAmt + "</span><br/>" +
"<span>Pending Amount</span><span class='right' id='popover-funding'>" + $pendAmt + "</span><br/>" +
"<span>Total Amount</span><span class='right' id='popover-funding'>" + $netAmt + "</span><br/><br/>" +
"<a href='#' data-toggle='modal' data-target='#project-funding-history-modal' id='funding-history-modal'><i class='glyphicon glyphicon-stats'></i> Project Budget & Expense Report</a>";
$('#funding-popover').attr('data-content', $popoverContent);
} else {
//Populate popover content
$popoverContent = "<span>Fund Amount</span><span class='right' id='popover-funding-" + $id + "'>" + $fundAmt + "</span><br/>" +
"<span>Pending Amount</span><span class='right' id='popover-funding-" + $id + "'>" + $pendAmt + "</span><br/>" +
"<span>Total Amount</span><span class='right' id='popover-funding-" + $id + "'>" + $netAmt + "</span><br/><br/>" +
"<a href='#' data-toggle='modal' data-target='#project-funding-history-modal-" + $id + "' id='funding-history-modal-" + $id + "'><i class='glyphicon glyphicon-stats'></i> Project Budget & Expense Report</a>";
$('#funding-popover-' + $id).attr('data-content', $popoverContent);
}
//popover tooltip
$popoverId = "";
if ($id == 0) {
$popoverId = '#funding-popover';
$fundingTable = '#project-funding-history-modal';
} else {
$popoverId = '#funding-popover-' + $id;
$fundingTable = '#project-funding-history-modal-' + $id;
}
var popover = $($popoverId).popover({ trigger: "manual", html: true, animation: true })
.on("mouseenter", function () {
var _this = this;
$(this).popover("show");
$(".popover").on("mouseleave", function () {
$(_this).popover('hide');
});
}).on("mouseleave", function () {
var _this = this;
setTimeout(function () {
if (!$(".popover:hover").length) {
$(_this).popover("hide");
}
}, 300);
});
//Build the funding history
$($fundingTable).find("tr:gt(0)").remove();
if (data.ProjectBudgetAndExpenseSummaryData.PostingBalance != null) {
$.each(data.ProjectBudgetAndExpenseSummaryData.PostingBalance, function (i, item) {
$row = "<tr><td>" + item.Account + "</td><td>" + item.AccountName + "</td><td>" + item.Budget + "</td><td>" + item.Enc + "</td><td>" + item.Ptd + "</td><td>" + item.Avl + "</td></tr>";
$($fundingTable).find('tbody').append($row);
});
}
$('#project-distribution-loading').hide();
if ($id == 0) {
$('#project-number-label').hide();
$('#project-distribution-details').show();
} else {
$('#project-number-label-' + $id).hide();
$('#project-distribution-details-' + $id).show();
}
return false;
},
error: function (data) {
alert("error");
return false;
}
});
在 jQuery 中是否可以选择具有“.typeahead”类的所有输入的预输入列表的第一项?
提前致谢。
【问题讨论】:
-
我不太清楚你的意思,但你试过
$( ".typeahead:first" )吗? -
好吧,我用项目编号重新填充预输入输入,我想触发预输入(就像用户选择了列表中的第一项)
-
“ajax”代码是什么样子的?
-
@Jigfors:我用 Ajax 代码更新了代码
-
不能在
$( document ).ready(function() {});上调用函数ore ajax调用吗?或者应该是$(document).load(function(){};!
标签: jquery twitter-bootstrap typeahead.js