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

时间:2014-11-28 14:28:32

标签: javascript jquery jquery-events

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

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

我正在使用

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

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

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:0)

此范围内的$(this)不会将输入作为对象返回,而是将整个页面作为对象返回...

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