【问题标题】:Property 'checked' does not exist on type 'TElement'类型“TElement”上不存在属性“已检查”
【发布时间】:2018-10-09 07:20:39
【问题描述】:
  • 我是打字稿的新手。
  • 我在小提琴中有一个工作原型,如果我使用它,它不会抛出任何错误。 http://jsfiddle.net/61ufvtpj/2/

  • 但在打字稿中我使用这一行 if(this.checked){ 它会抛出此错误 [ts] 类型 'TElement' 上不存在属性 'checked'。

  • 能否告诉我如何修复它,以便我以后自己修复它。
  • 在下面提供相关代码,在 gist 中提供完整代码。

https://gist.github.com/niniyzni/4faea080e53eb0c7155ddd8fb635a46c

 $(document).on('change', '#playerFlagCheck', function () {

            if(this.checked){ //[ts] Property 'checked' does not exist on type 'TElement'.
                $('#editIconplayer').addClass("gridUpdateIcon");
                alert("I am inside if");
            }else{
                alert("I am inside else");
                if(!$('#editIconplayer').hasClass("gridUpdateIcon")){
                    $('#editIconplayer').removeClass("gridUpdateIcon");
                }
            }

        });

【问题讨论】:

    标签: javascript jquery css angular typescript


    【解决方案1】:

    试试看。你也应该使用 prop 或 attr

     $(document).on('change', '#playerFlagCheck', function () {
           if($(this).is(':checked')) {
             $('#editIconplayer').addClass("gridUpdateIcon");
               alert("I am inside if");
             } else {
               alert("I am inside else");
               if(!$('#editIconplayer').hasClass("gridUpdateIcon")) {
                 $('#editIconplayer').removeClass("gridUpdateIcon");
               }
             }
    
    });
    

    【讨论】:

    • 嘿,它只适用于一行......但它应该适用于多行......如果我取消选择那个时间,图像也应该保留......知道如何解决它跨度>
    【解决方案2】:

    您的事件处理程序正在创建一个新上下文 to more about check me。所以,this 不再是指控制器本身。

    这一定能解决问题:

    const vm = this;
    $(document).on('change', '#playerFlagCheck', function () {
       if(vm.checked) {
         $('#editIconplayer').addClass("gridUpdateIcon");
           alert("I am inside if");
         } else {
           alert("I am inside else");
           if(!$('#editIconplayer').hasClass("gridUpdateIcon")) {
             $('#editIconplayer').removeClass("gridUpdateIcon");
           }
         }
    
    });
    

    【讨论】:

    • 嘿,谢谢您的回复...我更新了代码,但现在我收到一个错误,例如此属性已检查在类型 playerBulkUpdate 上不存在...。还有其他问题吗?跨度>
    • playerBulkUpdate 是您的控制器的名称?向我们展示更多代码(以及html)以帮助您。
    • hey export class playerBulkUpdate { 是类名gist.github.com/niniyzni/…
    • hey paapu 下面的代码仅适用于一行......但它应该适用于多行......如果我取消选择那个时间,图像也应该保留......知道如何修复它
    • 嘿,这里是 html gist.github.com/niniyzni/…
    猜你喜欢
    • 2018-09-07
    • 2021-01-15
    • 2019-04-21
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多