我是 ajax/jquery 新手。我正在开发一个以贷款号码作为输入的应用程序。当文本框字段失去焦点时,它应该通过从数据库获取信息来显示该特定贷款的信息。
我正在 MVC 中开发它。我的观点包括
LoanNumber = Textboxfield.
Contact Name = Textboxfield
Phone = Textboxfield.
下面是我的示例代码,它告诉我们贷款号码是否存在。
$("#LoanNumber").blur(function () {
var num = $("#LoanNumber").val();
var status = $("#divstatus");
$.post("/FnmaImport/CheckLoanNumber", { LoanNumber: num },
function (data) {
if (data == true) {
status.html("<font color=green>'<b> Loan Number " + num + "</b>' is available!</font>");
} else {
status.html("<font color=red>'<b> Loan Number " + num + "</b>' is not available!</font>");
}
});
});
现在我的问题是,我们是否需要在 Controller 中编写一个新方法来从数据库中检索信息并在 IF 条件为 true 时进行 ajax 调用?
最佳答案
只需使用 1 个 Controller 方法,例如...
/FnmaImport/GetLoanNumber
如果 num 存在则返回数据库中的信息,如果不存在则返回 false。
Controller 可以返回一些 JSON 之类的内容。
{
loadNumber: ####,
result: true|false, /* whether it exists or not */
name: "asdfadsfadsf",
date: "####-##-##"
}
$.post("/FnmaImport/GetLoanNumber", { LoanNumber: num },
function (data) {
if (data.result == true) {
status.html("<font color=green>'<b> Loan Number " + num + "</b>' is available!</font>");
alert(data.name + " " + data.date /* etc */);
} else {
status.html("<font color=red>'<b> Loan Number " + num + "</b>' is not available!</font>");
}
});
关于jquery - 文本框字段失去焦点后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18105925/