简体   繁体   English

类模式中的jQuery click函数

[英]Jquery click function in a class pattern

I can not get the function on page .click run unless I paste in the browser console. 除非粘贴在浏览器控制台中,否则无法在.click运行页上获得该功能。

in my class I have: 在我的课堂上,我有:

var myClass = function(){
   var toggleChecked = function(){
       $('#myCheckBoxId').click(function() {  //on click                
        if(this.checked) { // check select status
            $("tr td input").each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"               
            });
        }else{
            $("tr td input").each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                       
            });         
        }
    });
   }

 return {
    myClass: toggleChecked
 }
}()


 $(document).ready(function(){
    myClass.toggleChecked();
 });

when I run the application, function not charging, but if I copy and paste in the browser console, works, someone can give me an explanation of where I am going wrong? 当我运行该应用程序时,功能不收费,但是如果我将其复制并粘贴到浏览器控制台中可以正常工作,那么有人可以给我解释我哪里出错了吗?

Thank you 谢谢

Maybe you should try this way: 也许您应该这样尝试:

var myObject = {
    toggleChecked: function () {
        $('#myCheckBoxId').click(function () { //on click                
            if (this.checked) { // check select status
                $("tr td input").each(function () { //loop through each checkbox
                    this.checked = true; //select all checkboxes with class "checkbox1"               
                });
            } else {
                $("tr td input").each(function () { //loop through each checkbox
                    this.checked = false; //deselect all checkboxes with class "checkbox1"                       
                });
            }
        });
    }
}

$(document).ready(function () {
    myObject.toggleChecked();
});

Or you can define a class if you want: 或者,您可以根据需要定义一个类:

function MyClass () {
    this.toggleChecked = function () {
        $('#myCheckBoxId').click(function () { //on click                
            if (this.checked) { // check select status
                $("tr td input").each(function () { //loop through each checkbox
                    this.checked = true; //select all checkboxes with class "checkbox1"               
                });
            } else {
                $("tr td input").each(function () { //loop through each checkbox
                    this.checked = false; //deselect all checkboxes with class "checkbox1"                       
                });
            }
        });
    }
}


$(document).ready(function () {
    var obj = new MyClass();
    obj.toggleChecked();
});

DEMO 演示

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

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