我有打字稿代码

就这个

$('.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)=>字符串|数'。


在这个

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



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


我该如何解决?

最佳答案

在第一种情况下,请使用:

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


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

对于第二种情况,请使用:

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


要么

this.getAttribute('id')


代替

this.id


希望这为您服务。

09-25 22:29
查看更多