Javascript 无法读取未定义(TypeScript)的属性“indexOf”
我有cofeescript和工作代码这里是它的代码Javascript 无法读取未定义(TypeScript)的属性“indexOf”,javascript,jquery,typescript,coffeescript,Javascript,Jquery,Typescript,Coffeescript,我有cofeescript和工作代码这里是它的代码 window.load_autocomplete_fields = -> $('.airport_field').each -> $(this).autocomplete({ delay: 10, minLength: 0, source: ((request, response) -> $(this.element[0]).attr('data-req-t
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(); }
});
})
;
但现在我有了错误
我需要从选择列表中获取该行的值并将其粘贴到输入
如果$this.id.indexOf'origin'!==-一,{
我有错误
无法读取未定义的属性“indexOf”
我怎样才能解决它
谢谢你的帮助
我需要从选择列表中获取该行的值并将其粘贴到输入
如果$this.id.indexOf'origin'!==-1{
我有错误
无法读取未定义的属性“indexOf”
$这是一个jquery对象,您需要首先从中获取DOM对象
$(this)[0].id
或者干脆
this.id
i、 e
你为什么要麻烦呢?有在线工具,比如说是的。这很有帮助
if ($(this)[0].id.indexOf('origin') !== -1) {