使jQuery自动完成功能变得常见 [英] Make jquery autocomplete common

查看:72
本文介绍了使jQuery自动完成功能变得常见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC-C#应用程序,其中某些文本框具有jquery自动完成功能.每个视图都有自动完成代码,一些用于同一字段(不同的视图),还有一些用于不同的字段.

例如.

  • 发票\创建查看客户文本框
  • 付款\创建查看客户文本框
  • 付款\创建查看发票编号文本框

具有自动完成功能.一切都按预期工作.

这里是示例代码(在每个控件的每个视图中-这是给客户的)

  $(document).ready(function(){$(#CustomerName").autocomplete({来源:功能(请求,响应){$ .ajax({url:"@ Url.Action("GetCustomers","Invoices")),数据类型:"json",数据: {术语:request.term},成功:功能(数据){response($.map(data,function(val,item){返回 {标签:val.CustomerName,值:val.CustomerName,客户编号:val.CustomerId}}))}})},选择:函数(事件,用户界面){$(#CustomerId").val(ui.item.CustomerId);}});}); 

现在我想要的是,在需要在任何视图中的任何控件中使用此自动完成功能时,在Scripts文件夹中的文件中创建一个通用函数,并将其与参数一起调用.是否有实现该功能的方法?

我在视图的脚本部分尝试了 $(document).ready 函数以及其他几个选项,但未成功.

请至少让我知道

解决方案

 功能(elementId,controllerName,actionName,fieldName){$(#" + elementId +").autocomplete({来源:功能(请求,响应){$ .ajax({url:'api/'+ controllerName +'/'+ actionName +'',数据类型:"json",数据: {术语:request.term},成功:功能(数据){response($.map(data,function(val,item){返回 {标签:val [0],值:val [1],fieldName:val.fieldId}}))}})},选择:函数(事件,用户界面){$(#" + elementId +").val(ui.item.fieldName);}});} 

I have an MVC - C# app with jquery autocomplete feature for some text boxes. Each View has autocomplete code, some are for the same field (different views), and some different fields as well.

ex.

  • Invoice\Create View customer text box
  • Payment\Create View customer text box
  • Payment\Create View Invoice Number text box

having autocomplete feature. All working as expected.

Here is a sample code (which is in each view for each control - this is for Customer)

$(document).ready(function () {
    $("#CustomerName").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("GetCustomers", "Invoices")',
                datatype: "json",
                data: {
                    term: request.term
                },
                success: function (data) {
                    response($.map(data, function (val, item) {
                        return {
                            label: val.CustomerName,
                            value: val.CustomerName,
                            CustomerId: val.CustomerId
                        }
                    }))
                }
            })
        },
        select: function (event, ui) {
            $("#CustomerId").val(ui.item.CustomerId);
        }
    });
});

Now what I want is, create a common function in a file in Scripts Folder and call that, along with parameters, whenever I need this autocomplete feature to any control in any view.Is there a way to achieve it?

I tried the $(document).ready function in the view's script section, and few other options, but was unsuccessful.

Please let me know, at least whether this could be done or not

解决方案

function (elementId,controllerName,actionName,fieldName) {
    $("#"+elementId+"").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: 'api/'+controllerName+'/'+actionName+'',
                datatype: "json",
                data: {
                    term: request.term
                },
                success: function (data) {
                    response($.map(data, function (val, item) {
                        return {
                            label: val[0],
                            value: val[1],
                            fieldName: val.fieldId
                        }
                    }))
                }
            })
        },
        select: function (event, ui) {
            $("#"+elementId+"").val(ui.item.fieldName);
        }
    });
}

这篇关于使jQuery自动完成功能变得常见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆