简体   繁体   English

无法读取未定义的属性'indexOf'(TypeScript)

[英]Cannot read property 'indexOf' of undefined (TypeScript)

I have cofeescript with working code here is code of it 我有工作代码的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()

I need to rewrite it into typescript 我需要将其重写为打字稿

So I rewrite it like this 所以我这样改写

 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(); }
    });
  })
;

But now I have error. 但是现在我有错误。

I this row I need to get value from select list and paste it to input 我需要从选择列表中获取值并将其粘贴到输入中

if ($(this).id.indexOf('origin') !== -1) { 如果($(this).id.indexOf('origin')!== -1){

I have error 我有错误

Cannot read property 'indexOf' of undefined 无法读取未定义的属性'indexOf'

How I can solve it? 我该如何解决?

Thanks for help 感谢帮助

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 无法读取未定义的属性'indexOf'

$(this) is a jquery object, you need to get the DOM object from it first $(this)是一个jquery对象,您需要先从中获取DOM对象

$(this)[0].id

or simply 或简单地

this.id

ie

if ($(this)[0].id.indexOf('origin') !== -1) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM