简体   繁体   English

jquery this.id不工作

[英]jquery this.id not working

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')
        }


    });

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 ... 上面的代码适用于几个复选框....但我想在页面加载后执行检查不在$ (".input_check").change ....我试过...

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

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?? 我试图找到ID this.id但不是wrking ..我也试过(this).idthis.attr("id")仍然没有工作... :(我得到undefined无论如何要解决这个问题?

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

(this).id and this.attr("id") are not proper jQuery syntax for this situation. (this).idthis.attr("id")不适合这种情况的jQuery语法。 Should be 应该

$(this).attr('id') 

Fiddle 小提琴

Edit based on your comments 根据您的评论进行修改

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 小提琴

To get the ID of all :checked elements as an array: 要将所有:checked元素的ID作为数组获取:

var checked = $('.input_check:checked').map(function() {
    return this.id;
}).get();

try: 尝试:

$(this).attr('id')

and check if you have a ID attribute defined in the checkbox 并检查您是否在复选框中定义了ID属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM