jQuery this.id不起作用 [英] jquery this.id not working

查看:166
本文介绍了jQuery this.id不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是我的代码...

Hello people here is my code ...

    $ (".input_check").change (function (){

        if ($ (this).is (':checked')) {
            console.log(this.id + 'is checked')
        } else {
            console.log(this.id + 'is not checked')
        }


    });

上面的代码可以很好地用于多个复选框....但是我想在页面加载后执行检查,而不是在$ (".input_check").change ...之后.我尝试了...

the above code works fine for several check boxes ....But i want to perform check after the page load not after $ (".input_check").change.... I tried ...

 $ (function () {
if ($ (".input_check").is (':checked')) {
            console.log(this.id + 'is checked')
        }
)}

我正在尝试查找ID this.id,但没有崩溃..我也尝试了(this).idthis.attr("id")仍然无法正常工作... :(无论如何,都在获取undefined来解决此问题??

Iam trying to find the ID this.id but not wrking.. I also tried (this).id and this.attr("id") still not working... :( am getting undefined anyways to fix this??

UPDATE :: I have multiple checkboxes which are checked... I am trying to find multiple ids of those check boxes which are checked

推荐答案

(this).idthis.attr("id")在这种情况下不是正确的jQuery语法.应该是

(this).id and this.attr("id") are not proper jQuery syntax for this situation. Should be

$(this).attr('id') 

小提琴

根据您的评论进行编辑

好吧,那是什么阻止您在准备好文档时执行相同的操作?

Ok, then what stops you from doing the same on document ready?

 $(function() {
     $( ".input_check" ).each(function( index ) {
        if ($ (this).is (':checked')) {
            console.log($(this).attr('id')  + 'is checked')
        } else {
            console.log($(this).attr('id')  + 'is not checked')
        }
     });
 });

小提琴

Fiddle

这篇关于jQuery this.id不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆