jQuery自动完成源

时间:2013-08-06 13:17:06

标签: jquery-ui autocomplete

我正在同时在几个输入上添加自动完成功能,因此编写一个处理函数作为源。我想在我的处理函数中使用原点的id属性(触发动作的输入)。但似乎在自动完成中没有直接引用它......

$('#inputForm #supplier, #inputForm #label').autocomplete({
    source: function(request, response) {
        $.post("autocomplete.php", {id: ???, term: request.term}, success);
    }
});

有任何线索吗?

2 个答案:

答案 0 :(得分:2)

知道了!感谢理查德;)

 $(this.element).attr('id')

完整代码,万一有人会感兴趣:

$('input').autocomplete({
    source: function(request, response) {
        $.post("autocomplete.php", {origin: $(this.element).attr('id'), term: request.term}, success);
    }
});

答案 1 :(得分:0)

你可以使用

$(this).attr('id')

我建议您也为所有自动完成功能使用类,例如

.autocomplete

并为您的额外数据添加数据,例如

data-id, data-url

您可以使用

获取data-xxx属性的值
$(this).data('id') and $(this).data('url')
enter code here
$(".autocomplete")
.each(function () {
   $(this).autocomplete({
      source : function(request, response) {
    $.post($(this).data('url'), {origin: $(this.).data('id'), term:               request.term}, success);
}
       });
    });
相关问题