jQuery-获取调用元素ID [英] JQuery - Get calling element id

查看:287
本文介绍了jQuery-获取调用元素ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

在自动完成功能的源函数中,我想获取选择器的ID.有没有办法可以遍历调用堆栈并获得此权限? JQuery是否具有这种抽象级别?

In the source function of the autocomplete I want to get the selector's ID. Is there a way I can travese through the call stack and get this? Does JQuery have this level of abstraction?

为什么?

我将在页面上具有多个自动完成功能,并且每个自动完成功能在服务器端的处理方式都不同.我必须对源使用另一个函数.否则,我会使用URL +数据:很久以前= p

I will have multiple autocompletes on the page and each one will be handled differently on the server side. I have to use another function for the source. Otherwise I would have used a URL + data: a long time ago =p

JQuery版本

jquery-1.9.1

jquery-1.9.1

研究

我当然已经遍历了: JQuery API

Of course I've been all over this: JQuery API

如何获取元素ID从动态生成的表单元素与jQuery?

我认为很多这样的尝试都行不通,但现在我正处于试错的阶段.

A lot of these attempts I didn't think would work, but right now I'm at the point of trial and error.

$(this).attr('id'); -未定义

呼叫者函数名称

尽管我会尝试获取调用者函数的名称,并用它做点什么……似乎什么也没输出.

I though I'd try to get the caller functions name, and do something with it...doesn't seem to output anything.

附加到源函数(这很荒谬!将文本附加到函数?!我真的很拼命...)

Appending to the source function (this is absurd!!! Appending text to a function?! Really I'm desperate...)

 $("#inPdVehMk").autocomplete({
            source: autoCompletePost + "field="+$(this).attr('id'),
            minLength: 2,
            select: function(event, ui){
                alert(ui.label1);
                alert("value= " + ui.item.value + " id= "+ ui.item.id);
            }
        });

自动完成设置

    $("#inPdVehMk").autocomplete({
        source: autoCompletePost,
        minLength: 2,
        select: function(event, ui){
            alert(ui.label1);
            alert("value= " + ui.item.value + " id= "+ ui.item.id);
        }
    });

源功能

function autoCompletePost(request, response){
    //alert($(this).attr('id')); //this is where I'm testing to see the ids.
    $.post(AjaxPageAutoComplete, { searchTerm: request.term, field: 'inPdVehMk'}, //I want field to be dynamic depending on the calling selector.
        function(data) {
            var splitData = data.split("%");
            var json = jQuery.parseJSON(splitData[1].toString());

            if(data.search('autoCompleteError') !== -1 || data.length < 1){
                DialogBox('Div_ErrorMessage^Open^autoCompleteError');
            }else{
                response(json);
            }
        }
    );
}

推荐答案

我很尴尬地花了我多长时间才能弄清这一点.

I'm fairly embarrassed how long it took me to figure this out.

在源函数中使用此行以获取自动完成功能的ID.

Use this line in your source function to get the id of the autocomplete.

$(this.element).prop("id")

由于自动完成是一个jQuery小部件,因此this对象引用 widget 实例.要访问该元素,您需要遍历它的element属性.

Since autocomplete is a jquery widget, the this object refers to the widget instance. To get to the element, you need to go through it's element property.

JSFiddle

公平地说,除非您使用 Widget Factory .

To be fair, this isn't very well documented unless you are creating widgets using the Widget Factory.

这篇关于jQuery-获取调用元素ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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