jquery - 文本框字段失去焦点后?

标签 jquery asp.net-mvc-4

我是 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/

相关文章:

jquery - 如何在带有花式树的绑定(bind)下拉列表数据上显示加载图像

javascript - 使用 KnockoutJS 绑定(bind)动态创建的表条目

c# - ASP.Net MVC 添加寻呼机但找不到路由

.net - 如何将字符串连接到 MVC4 中模型返回的数据

javascript - 如何为 <img> 标签使用模糊事件

jquery - 如何使用 jQuery 实现类似 google 的日历?

jquery - MVC 应用程序无法识别 Bootstrap Datetimepicker

jQuery 日历 - 突出显示日期

jquery - 从具有相同类的所有元素中获取文本

内联代码的 Javascript 垃圾收集