如何使用此变量获取 typeahead.js 中输入的 id
How to use this variable to get the id of input in typeahead.js?
我是第一次使用typeahead,这对你们中的许多人来说应该是一个简单的问题。我有多个输入元素,我正在使用 typeahead。所有这些元素都具有相同的类。
<input class="typeahead" type="text" id="element_1" >
<input class="typeahead" type="text" id="element_2" >
当 typeahead 在任何元素上处于活动状态时,我需要确定它的 id。这就是我正在尝试的。
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'items',
displayKey: 'value',
source:populateItems()
});
var populateItems=function(){
return function findMatches(inp, cb) {
console.log("id: "+$(this).id); //this is where i need to find the id
jQuery.get("auto_suggestion.php",function(data){
// some work here ..
cb(res);
});
};
};
我试图在网上查看他们的例子和其他来源,但没有找到太多帮助。
不能从数据源函数访问元素。 this
指向Dataset
对象,并且没有对输入元素的引用。但是,您可以在循环中初始化 typeahead each
以便您可以访问当前输入元素:
$('.typeahead').each(function() {
$(this).typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'items',
displayKey: 'value',
source: populateItems(this.id)
})
});
function populateItems (id) {
return function findMatches(inp, cb) {
console.log("id: " + id);
// ...
};
};
试试...
$(this).attr("id");
。它使用 jQuery,但使用我一直使用的快速、简单的方法获取 Id 属性。
相关文章:
- <输入id=“;“到期”;type=“;月份;min=“;2016-05”>如何填写“;min”;属性与当前
- jquery如何检测当前输入id
- jQuery-如何从同一类中隐藏的输入id中获取不同的值
- 修改序列化表单函数以获取输入ID而不是名称
- 在innerHTML中包含变量值以创建输入id's和占位符
- 将输入id传递到jquery ui自动完成中的url
- 具有相同输入 ID 的多个表单仅使用 Ajax 发送第一个值
- 无法获取使用 event.target.id 触发事件的输入 ID
- 当输入id时,从json文件向警报添加一个图像
- 输入id以一个可变字符串开头
- 当用户在输入id=“”中填充文本时,如何调用函数java脚本;value_ 1”;然后按键盘上的“Enter”
- 如何连接函数参数和输入 ID
- 当我改变选择标记为什么不在输入id=“0”中显示数据时;消息”;
- 如何使用jquery将数字添加到输入ID名称中
- 使用值动态更改输入id-输入类型=范围
- Jquery插入表行并增加输入id
- 以 html 形式输入 ID,并在同一页面中从 MySQL 数据库加载相关数据
- 检测,自动将输入Id更改为当前Id名称
- 输入 id 时如何输出名称
- 从PHP调用JS函数时不能捕获输入ID