使用jQuery将参数传递给JavaScript对象事件

时间:2022-12-05 16:11:31

I know there are a LOT of similar question to this, but I've tried them all to no avail...so thanks for any help.

我知道有很多类似的问题,但我已经尝试过所有这些都没有用...所以感谢任何帮助。

WHAT I AM TRYING TO DO: I want to pass one-to-many parameters into each checkboxes 'click' event upon registration (see below).

我想做什么:我想在注册时将一对多参数传递给每个复选框'click'事件(见下文)。

WHAT WORKS: I can register the event WITHOUT any parameters, and the click event raises...but I need to pass-in a reference to the containing JavaScript grid object (for other reasons).

工作原理:我可以在没有任何参数的情况下注册事件,并且click事件会引发...但是我需要传入对包含JavaScript网格对象的引用(出于其他原因)。

WHAT FAILS I've tried various forms of "this.MutuallyExclusiveCheckBoxHandler = function(grid){}" to no avail.

什么失败我尝试过各种形式的“this.MutuallyExclusiveCheckBoxHandler = function(grid){}”无济于事。

ONE IDEA: I "think" currying may be the answer, but I don't quite know how to do it well-enough (yet).

一个想法:我“认为”curry可能是答案,但我不太清楚如何做得好(还)。

This area instantiates the grid and registers the checkboxes

该区域实例化网格并注册复选框

<script type="text/javascript">
<!--
    // CLASS
    function CommitImporterGrid() {
        // PROPERTIES
        this.Toaster = new Toaster();
        this.CheckBoxes = new Array();

        // METHODS
        this.RegisterMutuallyExclusiveCheckBox = function(clientId) {

            var checkBox = $j('input:checkbox#' + clientId);

            // HERE: "I need to pass a reference to the grid somehow"
            $j(checkBox).click(this.MutuallyExclusiveCheckBoxHandler);

            this.CheckBoxes.push(checkBox); // Append to array
        }
        this.MutuallyExclusiveCheckBoxHandler = function() {

            // The checkbox events break when I try to add a parameter.
            var myGrid = "I need to retreive the grid reference here somehow";

            if (!$j(this).is(':checked')) { // They clicked on the same checkbox
                this.checked = true;
                return;
            }

            // ...other code...
        }
    }

     // CLASS INSTANCE
     var myGrid = new CommitImporterGrid();

     // DOM EVENT: Document.Ready()
     $j(document).ready(function() {

         // DYNAMIC REGISTRATION
         myGrid.RegisterMutuallyExclusiveCheckBox('chkCommitImporter01');
         myGrid.RegisterMutuallyExclusiveCheckBox('chkCommitImporter02');
     });
-->
</script>

2 个解决方案

#1


1  

Use .bind() and pass event data (assuming your grid is the current object):

使用.bind()并传递事件数据(假设您的网格是当前对象):

// HERE: "I need to pass a reference to the grid somehow"
checkBox.bind('click', {grid: this}, this.MutuallyExclusiveCheckBoxHandler);

(you don't need to pass checkbox to jQuery, it is already a jQuery object)

(你不需要将复选框传递给jQuery,它已经是一个jQuery对象)

and change your method to:

并将您的方法更改为:

 this.MutuallyExclusiveCheckBoxHandler = function(event) {
      var mygrid = event.data.grid;
      //...
 }

(You can can access the event data via event.data)

(您可以通过event.data访问事件数据)

#2


0  

You need to add an anonymous function that calls your handler with your parameters, like this:

您需要添加一个匿名函数,使用您的参数调用您的处理程序,如下所示:

this.RegisterMutuallyExclusiveCheckBox = function(clientId) {
    var self = this;

    var checkBox = $j('input:checkbox#' + clientId);

    $j(checkBox).click(function() {
        self.MutuallyExclusiveCheckBoxHandler(parameters);
    });

    this.CheckBoxes.push(checkBox); // Append to array
};

Inside the event handler, this refers to the element that triggered the event.
I therefore use a separate self variable instead.

在事件处理程序内部,这指的是触发事件的元素。因此,我使用单独的自变量。

#1


1  

Use .bind() and pass event data (assuming your grid is the current object):

使用.bind()并传递事件数据(假设您的网格是当前对象):

// HERE: "I need to pass a reference to the grid somehow"
checkBox.bind('click', {grid: this}, this.MutuallyExclusiveCheckBoxHandler);

(you don't need to pass checkbox to jQuery, it is already a jQuery object)

(你不需要将复选框传递给jQuery,它已经是一个jQuery对象)

and change your method to:

并将您的方法更改为:

 this.MutuallyExclusiveCheckBoxHandler = function(event) {
      var mygrid = event.data.grid;
      //...
 }

(You can can access the event data via event.data)

(您可以通过event.data访问事件数据)

#2


0  

You need to add an anonymous function that calls your handler with your parameters, like this:

您需要添加一个匿名函数,使用您的参数调用您的处理程序,如下所示:

this.RegisterMutuallyExclusiveCheckBox = function(clientId) {
    var self = this;

    var checkBox = $j('input:checkbox#' + clientId);

    $j(checkBox).click(function() {
        self.MutuallyExclusiveCheckBoxHandler(parameters);
    });

    this.CheckBoxes.push(checkBox); // Append to array
};

Inside the event handler, this refers to the element that triggered the event.
I therefore use a separate self variable instead.

在事件处理程序内部,这指的是触发事件的元素。因此,我使用单独的自变量。