最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'indexOf' of undefined (TypeScript) - Stack Overflow

programmeradmin1浏览0评论

I have cofeescript with working code here is code of it

   window.load_autoplete_fields = ->
  $('.airport_field').each ->
    $(this).autoplete({
      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-autoplete")._renderItem = (ul, item) ->
      create_autoplete_item($(this.element[0]), ul, item)

    $('.airport_field').on 'autopleteselect', ->
      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_autoplete_fields:()=>JQuery;
    load_datepickers:()=>JQuery;
    load_datepickers_inline:()=>JQuery; 
    customCheckbox:(checkboxName:string) => JQuery;
    customCheckboxes:any;
    customRadio:(radioName:string)=>JQuery;
  }

    window.load_autoplete_fields = () =>
  $('.airport_field').each(function() {
    $(this).autoplete({
      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-autoplete")._renderItem = function(ul, item) {
      return create_autoplete_item($(this.element[0]), ul, item);
    };

    $('.airport_field').on('autopleteselect', 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) {

I have error

Cannot read property 'indexOf' of undefined

How I can solve it?

Thanks for help

I have cofeescript with working code here is code of it

   window.load_autoplete_fields = ->
  $('.airport_field').each ->
    $(this).autoplete({
      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-autoplete")._renderItem = (ul, item) ->
      create_autoplete_item($(this.element[0]), ul, item)

    $('.airport_field').on 'autopleteselect', ->
      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_autoplete_fields:()=>JQuery;
    load_datepickers:()=>JQuery;
    load_datepickers_inline:()=>JQuery; 
    customCheckbox:(checkboxName:string) => JQuery;
    customCheckboxes:any;
    customRadio:(radioName:string)=>JQuery;
  }

    window.load_autoplete_fields = () =>
  $('.airport_field').each(function() {
    $(this).autoplete({
      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-autoplete")._renderItem = function(ul, item) {
      return create_autoplete_item($(this.element[0]), ul, item);
    };

    $('.airport_field').on('autopleteselect', 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) {

I have error

Cannot read property 'indexOf' of undefined

How I can solve it?

Thanks for help

Share Improve this question asked Mar 21, 2018 at 13:35 BalanceBalance 5812 gold badges11 silver badges24 bronze badges 1
  • 1 Why do you bother? There are online tools for this, like js2coffee – Jeremy Thille Commented Mar 21, 2018 at 13:39
Add a ment  | 

1 Answer 1

Reset to default 4

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) is a jquery object, you need to get the DOM object from it first

$(this)[0].id

or simply

this.id

i.e.

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

评论列表(0)

  1. 暂无评论