[英]jQuery autocomplete: How to obtain HTML item's “id” attribute?
jQuery is new to me, thanks for bearing with me. jQuery对我来说是新手,感谢您的支持。
I have some text inputs in HTML: 我在HTML中有一些文本输入:
<input type="text" class="NameTextID" id="fn.1">
<input type="text" class="NameTextID" id="ln.1">
<input type="text" class="EmailAddressTextID" id="ea.1">
Two inputs are of class NameTextID
and one is of class EmailAddressTextID
. 两个输入属于
NameTextID
类 ,一个输入属于EmailAddressTextID
类 。 Notice each id is unique. 请注意,每个ID都是唯一的。
I then would like to leverage jQuery's autocomplete function on any DOM item with a class from above. 然后,我想对具有上述类的任何DOM项目利用jQuery的自动完成功能。
$(document).ready(function() {
$(function() {
$( ".NameTextID" ).autocomplete({
source: recEngineNames,
minLength: recMinCharCount,
select: recEngine
});
$( ".EmailAddressTextID" ).autocomplete({
source: recEngineEmails,
minLength: recMinCharCount,
select: recEngine,
});
});
});
Now, in the function recEngine: 现在,在函数recEngine中:
var recEngine = function(event, ui) {
var selected_value = ui.item.value
var selected_id = ????
}
jQuery passes two parameters, how can I obtain the id if the ui item that activated the recEngine function? jQuery传递两个参数,如果激活了recEngine函数的ui项,如何获取ID ?
Thanks in advance! 提前致谢!
You can just do:- 您可以这样做:
var selected_id = this.id;
where this
refers to the current element being referred to. 其中
this
指的是当前元件被提及。
您可以像这样ui.item.id
它: ui.item.id
First thing you can use multi selectors by separating by a comma ','
. 首先,您可以使用多个选择器,以逗号
','
分隔。
Then to get the element where that fired the event you can use the key word this
or event.target
. 然后,要获取触发事件的元素,可以使用关键字
this
或event.target
。 To get its id you can use $(event.target).attr('id')
要获取其ID,可以使用
$(event.target).attr('id')
$(event.target).attr('id') $(event.target).attr( 'ID')
$(document).ready(function() {
$(function() {
$( ".NameTextID, .EmailAddressTextID" ).autocomplete({
source: recEngineNames,
minLength: recMinCharCount,
select: recEngine
});
});
});
var recEngine = function(event, ui) {
var selected_value = ui.item.value;
var selected_id = $(event.target).attr('id');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.