【问题标题】:Cannot read property 'indexOf' of undefined (TypeScript)无法读取未定义的属性“indexOf”(TypeScript)
【发布时间】:2018-08-30 15:49:48
【问题描述】:

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

但现在我有错误。

我需要从选择列表中获取值并将其粘贴到输入中

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

我有错误

无法读取未定义的属性“indexOf”

我该如何解决?

感谢您的帮助

【问题讨论】:

  • 你为什么要打扰?有这方面的在线工具,例如js2coffee

标签: javascript jquery typescript coffeescript


【解决方案1】:

我需要从选择列表中获取值并将其粘贴到输入中

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

我有错误

无法读取未定义的属性“indexOf”

$(this)是一个jquery对象,需要先从中获取DOM对象

$(this)[0].id

或者干脆

this.id

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

【讨论】:

  • 是的。有帮助
猜你喜欢
  • 1970-01-01
  • 2014-09-14
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2020-01-17
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多