var TourItinerary = {
EXCURSIONS:{
},
init:function(){
console.log('Initializing...');
this.initializeElements();
},
initializeElements:function(){
console.log('init elements...');
$("select[id^='start_city_']").change(function(){
var day_id = $(this).attr('id').replace("start_city_","");
getPossibleExcursions(day_id);
});
},
getPossibleExcursions: function(day_id){
console.log('Day Id ='+day_id);
}
};
如何在下拉列表更改事件中调用 getPossibleExcursions(day_id)
函数?
它总是显示:
未捕获的ReferenceError:未定义getPossibleExcursions。
答案 0 :(得分:1)
您必须为某些变量指定this
以供将来参考。定义更改事件时,this
引用要添加事件的DOM对象,而不是实际对象。因此,将this
分配给某些变量,比如self
,并在on change函数中重复使用它。
现在您可以使用self
来引用对象,使用this
来获取DOM对象
var TourItinerary = {
EXCURSIONS:{
},
init:function(){
console.log('Initializing...');
this.initializeElements();
},
initializeElements:function(){
console.log('init elements...');
var self = this;
$("select[id^='start_city_']").change(function(){
var day_id = $(this).attr('id').replace("start_city_","");
self.getPossibleExcursions(day_id);
console.log("value = " + $(this).val());
});
},
getPossibleExcursions: function(day_id){
console.log('Day Id ='+day_id);
}
};
$(document).ready(function(){
TourItinerary.init();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="start_city_123">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
&#13;