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

javascript - Argument of type '() => JQuery<HTMLElement>' is not assignable to parameter of

programmeradmin0浏览0评论

I have ts code to use with jquery autoplete

Here is code

    function 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) {
        const id = this.id.split('_')[2];
        return $(`#search_legs_${id}_destination_text`).focus();
      }
    });
    return $('.airport_field').focus(function() {
      if (!$(this).val()) { return $(this).val(' ').keydown(); }
    });
  });
  }

When I try to pile it I have this error

Argument of type '() => JQuery' is not assignable to parameter of type '(this: HTMLElement, index: number, element: HTMLElement) => false | void'. Type 'JQuery' is not assignable to type 'false | void'. Type 'JQuery' is not assignable to type 'void'.

in this row $('.airport_field').each(function() {

Here is HTML

<ul>
<li>
  <div class="header"><%= _('Från') %></div>
  <%= text_field_tag 'search[legs][0][origin_text]', @default_lsp[:legs][0][:origin_city][:return],
                     class: 'field-1 airport_field', placeholder: _('Från'),
                     "data-source" => '/autoplete/origin/flight',
                     "data-id-element" => '#search_legs_0_origin_id' %>
  <%= hidden_field_tag 'search[legs][0][origin_id]', @default_lsp[:legs][0][:origin_id] %>
  <div class="quick-destinations hide-small"><%= generate_quick_links_html.html_safe %></div>
</li>
<li class="icon"><%= image_tag 'shared/right_left_arrow.png', class: 'right_left_arrow', "data-number" => 0 %></li>
<li>
  <div class="header"><%= _('Till') %></div>
  <%= text_field_tag 'search[legs][0][destination_text]', @default_lsp[:legs][0][:destination_city][:return],
                     class: 'field-3 airport_field', placeholder: _('Till'),
                     "data-source" => '/autoplete/destination/flight',
                     "data-id-element" => '#search_legs_0_destination_id' %>
  <%= hidden_field_tag 'search[legs][0][destination_id]', @default_lsp[:legs][0][:destination_id] %>
</li>

How I can solve it?

I have ts code to use with jquery autoplete

Here is code

    function 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) {
        const id = this.id.split('_')[2];
        return $(`#search_legs_${id}_destination_text`).focus();
      }
    });
    return $('.airport_field').focus(function() {
      if (!$(this).val()) { return $(this).val(' ').keydown(); }
    });
  });
  }

When I try to pile it I have this error

Argument of type '() => JQuery' is not assignable to parameter of type '(this: HTMLElement, index: number, element: HTMLElement) => false | void'. Type 'JQuery' is not assignable to type 'false | void'. Type 'JQuery' is not assignable to type 'void'.

in this row $('.airport_field').each(function() {

Here is HTML

<ul>
<li>
  <div class="header"><%= _('Från') %></div>
  <%= text_field_tag 'search[legs][0][origin_text]', @default_lsp[:legs][0][:origin_city][:return],
                     class: 'field-1 airport_field', placeholder: _('Från'),
                     "data-source" => '/autoplete/origin/flight',
                     "data-id-element" => '#search_legs_0_origin_id' %>
  <%= hidden_field_tag 'search[legs][0][origin_id]', @default_lsp[:legs][0][:origin_id] %>
  <div class="quick-destinations hide-small"><%= generate_quick_links_html.html_safe %></div>
</li>
<li class="icon"><%= image_tag 'shared/right_left_arrow.png', class: 'right_left_arrow', "data-number" => 0 %></li>
<li>
  <div class="header"><%= _('Till') %></div>
  <%= text_field_tag 'search[legs][0][destination_text]', @default_lsp[:legs][0][:destination_city][:return],
                     class: 'field-3 airport_field', placeholder: _('Till'),
                     "data-source" => '/autoplete/destination/flight',
                     "data-id-element" => '#search_legs_0_destination_id' %>
  <%= hidden_field_tag 'search[legs][0][destination_id]', @default_lsp[:legs][0][:destination_id] %>
</li>

How I can solve it?

Share Improve this question edited Mar 20, 2018 at 15:20 Maistrenko Vitalii 1,0121 gold badge10 silver badges16 bronze badges asked Mar 20, 2018 at 13:03 BalanceBalance 5812 gold badges11 silver badges24 bronze badges 2
  • Please show your HTML. – Smit Raval Commented Mar 20, 2018 at 13:04
  • Updated ment@SmitRaval – Balance Commented Mar 20, 2018 at 13:11
Add a ment  | 

1 Answer 1

Reset to default 3

In this line:

return $('.airport_field').focus(function () {
      if (!$(this).val()) { return $(this).val(' ').keydown(); }
    });

Why are you returning the result of that call? The each() method doesn't want you to return anything unless you want to stop the iteration early, in which case you should return false. That's what the error is telling you:

Argument of type '() => JQuery' is not assignable to parameter of type '(this: HTMLElement, index: number, element: HTMLElement) => false | void'. Type 'JQuery' is not assignable to type 'false | void'. Type 'JQuery' is not assignable to type 'void'.

means

The function is returning a JQuery, which isn't appropriate. It should return false or void, and JQuery isn't either one of those. Specifically, it's not void.

Maybe you should change the above line to just

$('.airport_field').focus(function () {
    if (!$(this).val()) { return $(this).val(' ').keydown(); }
  });

without the return and see if it works.

Hope that helps. Good luck.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论