【问题标题】:Argument of type 'true' is not assignable to parameter of type'true' 类型的参数不能分配给类型参数
【发布时间】:2018-03-28 21:53:45
【问题描述】:

我有打字稿代码

这里是

$('.Switch').on('click', function() {
if ($(this).hasClass('On')) {
  $(this).parent().find('input:checkbox').attr('checked', true);
  $(this).removeClass('On').addClass('Off');
  $(this).parent().find('input:hidden').val(0);
  $(".filter_stops").each(function() {
    if ($(this).slider( "option", "value" ) === 0) {
      $(this).off('slidechange');
      $(this).slider( "option", "value", 99 );
      $(`#stops_${this.id.split('_')[1]}`).val(`Max ${$( this ).slider("value")} ${__('byten')}`);
      $(`#filter_stops_${this.id.split('_')[1]}`).val($( this ).slider("value"));
      $(this).on('slidechange', FilterFunctions.onFilterChange);
    }
  });

在这段代码中我有 2 个错误

在这一行$(this).parent().find('input:checkbox').attr('checked', true);我有

'true' 类型的参数不能分配给 '(index: number, attr: string) => string | 类型的参数号码”。

在这个

 $(`#stops_${this.id.split('_')[1]}`).val(`Max ${$( this ).slider("value")} ${__('byten')}`);

类型“TElement”上不存在属性“id”。

我该如何解决?

【问题讨论】:

  • 请分享您的html代码
  • 使用$(this).parent().find('input:checkbox').prop('checked', true); 而不是$(this).parent().find('input:checkbox').attr('checked', true);
  • 是的。它修复了第一个错误。但是我能用第二个做什么?@SupunFictionPraneeth

标签: javascript jquery typescript


【解决方案1】:

在第一种情况下,使用:

$(this).parent().find('input:checkbox').prop('checked', true);

因为使用attr,您只能获得属性的初始值(如果已设置)。 prop 给出属性的当前值。

对于第二种情况,使用:

$(this).attr('id').

this.getAttribute('id')

而不是

this.id

希望这对你有用。

【讨论】:

  • 如果我使用this.attr('id').我有Property 'attr' does not exist on type 'TElement'.
  • 我的错误。我纠正了它。你有两种选择,一种使用 jQuery,一种没有
  • 似乎有帮助。谢谢
猜你喜欢
  • 2021-01-05
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-26
  • 2021-06-19
  • 2021-12-13
  • 2021-08-19
  • 2020-06-25
相关资源
最近更新 更多