无法读取未定义的 属性 'indexOf' (TypeScript)
Cannot read property 'indexOf' of undefined (TypeScript)
我有带有工作代码的 cofeescript,这里是它的代码
window.load_autocomplete_fields = ->
$('.airport_field').each ->
$(this).autocomplete({
delay: 10,
minLength: 0,
source: ((request, response) ->
$(this.element[0]).attr('data-req-term', request.term)
$.ajax {
url: $(this.element[0]).attr('data-source'),
dataType: "json",
data: {
term: request.term
},
success: ((data) ->
results = []
$.map(data.cities, (value, key) ->
results.push(value)
$.map(value.airports, (value2, key2) -> results.push(value2))
)
$.map(data.airports, (value, key) -> results.push(value))
response(results)
),
error: (-> response([]))
}
return null
),
focus: ((event, ui) ->
return false
),
select: ((event, ui) ->
qel = $(event.currentTarget)
qel.val(ui.item.fullname)
$(qel.attr('data-id-element')).val(ui.item.id)
return false
)
}).data("ui-autocomplete")._renderItem = (ul, item) ->
create_autocomplete_item($(this.element[0]), ul, item)
$('.airport_field').on 'autocompleteselect', ->
if this.id.indexOf('origin') != -1
id = this.id.split('_')[2]
$("#search_legs_#{id}_destination_text").focus()
$('.airport_field').focus ->
$(this).val(' ').keydown() unless $(this).val()
我需要把它重写成打字稿
所以我改写成这样
interface Window {
load_autocomplete_fields:()=>JQuery;
load_datepickers:()=>JQuery;
load_datepickers_inline:()=>JQuery;
customCheckbox:(checkboxName:string) => JQuery;
customCheckboxes:any;
customRadio:(radioName:string)=>JQuery;
}
window.load_autocomplete_fields = () =>
$('.airport_field').each(function() {
$(this).autocomplete({
delay: 10,
minLength: 0,
source(request, response) {
$(this.element[0]).attr('data-req-term', request.term);
$.ajax({
url: $(this.element[0]).attr('data-source'),
dataType: "json",
data: {
term: request.term
},
success(data) {
const results = [];
$.map(data.cities, function(value, key) {
results.push(value);
return $.map(value.airports, (value2, key2) => results.push(value2));
});
$.map(data.airports, (value, key) => results.push(value));
return response(results);},
error() { return response([]); }
});
return null;},
focus(event, ui) {
return false;},
select(event, ui) {
const qel = $(event.currentTarget);
qel.val(ui.item.fullname);
$(qel.attr('data-id-element')).val(ui.item.id);
return false;}
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return create_autocomplete_item($(this.element[0]), ul, item);
};
$('.airport_field').on('autocompleteselect', function() {
if ($(this).id.indexOf('origin') !== -1) {
let id = $(this).id.split('_')[2];
return $(`#search_legs_${id}_destination_text`).focus();
}
});
$('.airport_field').focus(function () {
if (!$(this).val()) { return $(this).val(' ').keydown(); }
});
})
;
但现在我有错误。
我这一行需要从 select 列表中获取值并将其粘贴到输入
if ($(this).id.indexOf('origin') !== -1) {
我有错误
Cannot read property 'indexOf' of undefined
我该如何解决?
感谢帮助
I this row I need to get value from select list and paste it to input
if ($(this).id.indexOf('origin') !== -1) {
I have error
Cannot read property 'indexOf' of undefined
$(this)
是一个jquery对象,需要先从中得到DOM对象
$(this)[0].id
或者干脆
this.id
即
if ($(this)[0].id.indexOf('origin') !== -1) {
我有带有工作代码的 cofeescript,这里是它的代码
window.load_autocomplete_fields = ->
$('.airport_field').each ->
$(this).autocomplete({
delay: 10,
minLength: 0,
source: ((request, response) ->
$(this.element[0]).attr('data-req-term', request.term)
$.ajax {
url: $(this.element[0]).attr('data-source'),
dataType: "json",
data: {
term: request.term
},
success: ((data) ->
results = []
$.map(data.cities, (value, key) ->
results.push(value)
$.map(value.airports, (value2, key2) -> results.push(value2))
)
$.map(data.airports, (value, key) -> results.push(value))
response(results)
),
error: (-> response([]))
}
return null
),
focus: ((event, ui) ->
return false
),
select: ((event, ui) ->
qel = $(event.currentTarget)
qel.val(ui.item.fullname)
$(qel.attr('data-id-element')).val(ui.item.id)
return false
)
}).data("ui-autocomplete")._renderItem = (ul, item) ->
create_autocomplete_item($(this.element[0]), ul, item)
$('.airport_field').on 'autocompleteselect', ->
if this.id.indexOf('origin') != -1
id = this.id.split('_')[2]
$("#search_legs_#{id}_destination_text").focus()
$('.airport_field').focus ->
$(this).val(' ').keydown() unless $(this).val()
我需要把它重写成打字稿
所以我改写成这样
interface Window {
load_autocomplete_fields:()=>JQuery;
load_datepickers:()=>JQuery;
load_datepickers_inline:()=>JQuery;
customCheckbox:(checkboxName:string) => JQuery;
customCheckboxes:any;
customRadio:(radioName:string)=>JQuery;
}
window.load_autocomplete_fields = () =>
$('.airport_field').each(function() {
$(this).autocomplete({
delay: 10,
minLength: 0,
source(request, response) {
$(this.element[0]).attr('data-req-term', request.term);
$.ajax({
url: $(this.element[0]).attr('data-source'),
dataType: "json",
data: {
term: request.term
},
success(data) {
const results = [];
$.map(data.cities, function(value, key) {
results.push(value);
return $.map(value.airports, (value2, key2) => results.push(value2));
});
$.map(data.airports, (value, key) => results.push(value));
return response(results);},
error() { return response([]); }
});
return null;},
focus(event, ui) {
return false;},
select(event, ui) {
const qel = $(event.currentTarget);
qel.val(ui.item.fullname);
$(qel.attr('data-id-element')).val(ui.item.id);
return false;}
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return create_autocomplete_item($(this.element[0]), ul, item);
};
$('.airport_field').on('autocompleteselect', function() {
if ($(this).id.indexOf('origin') !== -1) {
let id = $(this).id.split('_')[2];
return $(`#search_legs_${id}_destination_text`).focus();
}
});
$('.airport_field').focus(function () {
if (!$(this).val()) { return $(this).val(' ').keydown(); }
});
})
;
但现在我有错误。
我这一行需要从 select 列表中获取值并将其粘贴到输入
if ($(this).id.indexOf('origin') !== -1) {
我有错误
Cannot read property 'indexOf' of undefined
我该如何解决?
感谢帮助
I this row I need to get value from select list and paste it to input
if ($(this).id.indexOf('origin') !== -1) {
I have error
Cannot read property 'indexOf' of undefined
$(this)
是一个jquery对象,需要先从中得到DOM对象
$(this)[0].id
或者干脆
this.id
即
if ($(this)[0].id.indexOf('origin') !== -1) {