Problem
在自动完成的源函数中,我想获取选择器的 ID。有没有办法可以遍历调用堆栈并得到这个?做JQuery
有这个抽象级别吗?
Why?
我将在页面上有多个自动完成功能,并且每个自动完成功能将在服务器端以不同的方式处理。我必须使用另一个函数作为源。否则我会使用 URL + 数据:很久以前 =p
jQuery 版本
jquery-1.9.1
Research
当然,我一直在关注这个:jQuery API http://api.jqueryui.com/autocomplete/#option-source
如何使用jquery从动态生成的表单元素中获取元素Id? https://stackoverflow.com/questions/6106443/how-to-get-element-id-from-dynamically-generated-form-elements-with-jquery
我认为很多尝试都行不通,但现在我正处于反复试验的阶段。
$(this).attr('id');- 不明确的
调用者函数名称 https://stackoverflow.com/questions/5750895/is-there-a-way-to-get-the-name-of-the-caller-function-within-the-callee
我虽然尝试获取调用者函数名称,并用它做一些事情......似乎没有输出任何内容。
附加到源函数(这太荒谬了!!!将文本附加到函数?!真的我很绝望......)
$("#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);
}
}
);
}