在类选择器中传递 jQuery this.id

Passing jQuery this.id in class selector

本文关键字:jQuery this id 选择器      更新时间:2023-09-26

我有两个不同的输入字段,它使用typeahead。

在第一个字段中,我们输入国家/地区,在其他字段中,我们应该选择城市,具体取决于国家/地区。所以我正在制作一个javascript/jQuery函数,以传递字段ID,知道我选择了3对国家+城市对中的哪一对。

我正在使用

 $('.demoTypeAhead').typeahead({
     source: getCityForCountry($(this).id)
 });

在函数 getCityForCountry 中,我想传递一个特定于字段的 id,但 $(this) 或这都不起作用,因为它返回整个 DOM 对象。

jQuery 对象没有 .id 属性。你要么想要

this.id // or
$(this).prop("id") // or even
$(this).attr("id")

$(this) 在此作用域上不会将输入作为对象返回,而是将整个页面作为对象返回...

刚刚为它做了jsfiddle: Jsfiddle http://jsfiddle.net/s1d8nmqr/3/当我选择第一个输入时,我想重新加载 typeahead 的数据源以进行第二个输入(2 级选择)