简体   繁体   English

jQuery动态事件绑定

[英]jQuery dynamic event binding

I think this is a pretty basic question, but I'm struggling with this problem. 我认为这是一个非常基本的问题,但是我正在为这个问题而苦苦挣扎。

On my form I have an option to dynamically add a new row to the table, which is simple enough to do with jQuery. 在我的表单上,我可以选择向表中动态添加新行,这对于jQuery而言非常简单。 On one of the elements, I need to bind an event, also relatively straightforward. 在其中一个元素上,我需要绑定一个事件,也相对简单。 The part I'm having problems with is the event that needs to be bound has to take a parameter. 我遇到问题的部分是需要绑定的事件必须带有参数。 So, the code I've got: 因此,我得到的代码是:

$(newrow).find("select").each(function () {
        var ochange = function(handle){
                var thisselect = "select" + handle;
                var thislink = "link" + handle;
                var thiscollate = "collate" + handle;
                if(document.getElementById(thisselect).value == 4){
                    document.getElementById(thislink).style.visibility = 'visible';
                }
                else{
                    document.getElementById(thislink).style.visibility = 'hidden';
                    document.getElementById(thiscollate).value = "";
                }
        };
        $(this).change(ochange);
        $(this).attr("name", "select" + numfields);
        $(this).attr("id", "select" + numfields);
    });

The event binds as expected, but the "handle" variable doesn't register (of course because I'm pretty sure this is incorrect syntax). 该事件按预期方式绑定,但是“ handle”变量未注册(当然,因为我敢肯定这是不正确的语法)。 Also, this function that I want to bind to the dynamic form element is actually already defined, so if there is a way I can call an already-defined function (that takes a parameter) I'd love to hear about it. 另外,实际上我已经定义了要绑定到动态表单元素的函数,因此,如果有一种方法可以调用已经定义的函数(带有参数),我很乐意听到。

In your code, 'handle' variable is a jQuery object, representing event for 'select' widget changing. 在您的代码中,“ handle”变量是一个jQuery对象,表示“ select”小部件更改的事件。 If you want to get DOM object which fired this event, use handle.target attribute. 如果要获取引发此事件的DOM对象,请使用handle.target属性。 For instance to get its name, use handle.target attribute. 例如,要获取其名称,请使用handle.target属性。 To get jQuery object for target - $(handle.target). 要获取目标的jQuery对象-$(handle.target)。

Full reference for Event objects is here 事件对象的完整参考在这里

The first parameter of an event handler function is the event object . 事件处理函数的第一个参数是事件对象

If I understand correctly, basically you want to pass a value to the ochange function. 如果我理解正确,那么基本上您想将一个值传递给ochange函数。

You could declare the variable handle in the scope of the each callback function, and use its value on the ochange function, since you have jQuery, you could use it in that function, and avoid the getElementById calls: 您可以在每个回调函数的范围内声明变量handle ,并在ochange函数上使用它的值,因为有了jQuery,就可以在该函数中使用它,并避免getElementById调用:

$(newrow).find("select").each(function () {

  var handle = "someValue"; // this variable will be accessible inside ochange

  var ochange = function(){
    var thisselect = "select" + handle;
    var thislink = "link" + handle;
    var thiscollate = "collate" + handle;
    if($(thisselect).val() == 4){
      $(thislink).css('visibility', 'visible');
    }
    else{
      $(thislink).css('visibility', 'hidden');
      $(thiscollate).val('');
    }
  };

  $(this).change(ochange);
  $(this).attr("name", "select" + numfields);
  $(this).attr("id", "select" + numfields);
});

You could supply a "helper" function: 您可以提供“帮助程序”功能:

 $(this).change( function (evt)  {
    ochange('someVal')
  });

Another option which is kind of complex but simple at the same time is the jquery.hitch plugin (which something of the sort is going into jQuery 1.3.3?) 另一个复杂但同时简单的选项是jquery.hitch插件(jQuery 1.3.3 中将使用哪种类型的插件?)

With it you could do something like this: 有了它,您可以执行以下操作:

  var obj = {
     attr: "blah",
     myFunc: function(arg){
       console.log(this.attr + " " + arg);
     }
  }
  $(this).change( $.hitch( obj, "myFunc", "wheee") )

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

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