繁体   English   中英

jquery this.id不工作

[英]jquery this.id not working

你好,这里的人是我的代码......

    $ (".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 ....我试过...

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

我试图找到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).idthis.attr("id")不适合这种情况的jQuery语法。 应该

$(this).attr('id') 

小提琴

根据您的评论进行修改

那么,是什么阻止你在准备文件时做同样的事情?

 $(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')
        }
     });
 });

小提琴

要将所有:checked元素的ID作为数组获取:

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

尝试:

$(this).attr('id')

并检查您是否在复选框中定义了ID属性

暂无
暂无

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

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