我的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()
我需要将其重写为typescript
所以我像这样重写它
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(); }
});
})
;
但现在我有错误。
我这一行我需要从选择列表中获取值并将其粘贴到输入
if($(this).id.indexOf('origin')!== -1){
我有错误
无法读取未定义
的属性'indexOf'
我如何解决?
感谢您的帮助
答案 0 :(得分:4)
我这一行我需要从选择列表中获取值并将其粘贴到输入
if ($(this).id.indexOf('origin') !== -1) {
我有错误
无法读取属性" indexOf'未定义的
$(this)
是一个jquery对象,你需要先从它获取DOM对象
$(this)[0].id
或只是
this.id
即
if ($(this)[0].id.indexOf('origin') !== -1) {