我如何获得控制调用jquery的自动完成功能的ID? [英] How do I get the id of the control calling the jquery autocomplete function?

查看:107
本文介绍了我如何获得控制调用jquery的自动完成功能的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要跨几个输入框使用jQuery的自动完成插件。为了保持我的code DRY我想扳平自动完成功能的一类,并自动完成功能内通过已调用它作为一个额外的参数字段的ID。我所挣扎是获取已调用函数的字段的ID。我在做什么例子code是以下;任何人都可以看到这个问题?

  $(类名。)自动完成('<%= Url.Action(法,控制器)%GT;',{
            数据类型:JSON,
            解析:功能(数据){
                VAR行=新的Array();
                对于(VAR I = 0; I< data.length;我++){
                    行[i] = {数据:数据[I],值:数据[I],结果是:数据[I]};
                }
                返回行;
            },
            formatItem:函数(行){
                返回行;
            },
            extraParams:{
                附加:功能(){
                    VAR字段1 = $(本).attr('身份证');
                    VAR字段2 = $(#字段1)VAL()。
                    VAR字段2 = $(#字段2)VAL()。
                    VAR字段3 = $(#字段3)VAL()。
                    返回字段1 +$+字段2 +$+字段3 +$+字段4;
                 }
            },
            延迟:40,
            自动填充:真实,
            selectFirst:假的,
            多:假的
        });


解决方案

您确定这个传递到您的extraParams功能?如果没有,那么你可以使用这样的:

  $(类名)。每个(函数(){
   VAR EL = $(本);
   .....
       附加:功能(){
            变种字段1 = el.attr(ID);
       }
});

这将允许您自动完成各绑了自己的特定元素。

I want to use the jquery autocomplete plugin across several input boxes. In order to keep my code DRY I want to tie the autocomplete function to a class and within the autocomplete function pass the id of the field that has called it as an extra parameter. What I am struggling with is obtaining the id of the field that has called the function. Example code of what I am doing is below; can anyone see the problem?

        $(".className").autocomplete('<%=Url.Action("Method", "Controller") %>', {
            dataType: 'json',
            parse: function(data) {
                var rows = new Array();
                for (var i = 0; i < data.length; i++) {
                    rows[i] = { data: data[i], value: data[i], result: data[i] };
                }
                return rows;
            },
            formatItem: function(row) {
                return row;
            },
            extraParams: {
                extra: function() {
                    var Field1 = $(this).attr('id');
                    var Field2 =  $("#Field1").val();
                    var Field2 =  $("#Field2").val();
                    var Field3 =  $("#Field3").val();
                    return Field1 + "$" + Field2 + "$" + Field3 + "$" + Field4; 
                 }
            },
            delay: 40,
            autofill: true,
            selectFirst: false,
            multiple: false
        });

解决方案

Are you sure this is passed to your extraParams function? If not then you can use something like:

$(".className").each(function(){
   var el = $(this);
   .....
       extra: function() {
            var Field1 = el.attr(id);
       }
});

This will allow you to tie each autocomplete to its own specific element.

这篇关于我如何获得控制调用jquery的自动完成功能的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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