简体   繁体   English

如何在文本框上附加onclick事件?

[英]How to attach onclick event on textboxes?

在此处输入图片说明 Hi, I want to attach on click event on Text Box with value A in attachment and same with B. I am using dojo toolkit with the following code: 嗨,我想附加文本框上的单击事件,附件中的值为A,与B相同。我正在使用以下代码的dojo工具箱:

  window.onload = function () {

         dojo.query('.test').onclick(function(){
              alert('test');

            });
};

I have a table control having two column name and value both columns contain textboxes.On column name textbox i have added a class with name 'test'.The table rows increase dynamically as shown in attachment. 我有一个具有两个列名和一个值的表控件,两个列都包含文本框。在列名文本框上,我添加了一个名为'test'的类。表行动态增加,如附件所示。 Now my concern is these textboxes increase dynamically how can i attach event on these textbox.On signle textbox this is working fine but here table rows increase and how can i find particular textbox with same class? 现在我关心的是这些文本框是如何动态增加的,如何在这些文本框上附加事件。在Signle文本框上,这可以正常工作,但是表行增加了,如何找到具有相同类的特定文本框? Can anyone help me? 谁能帮我?

define the function separately. 分别定义功能。

var myFunc = function() {
 alert('test');
};

Now, use it, to attach to text boxes (like you have already done) 现在,使用它来附加到文本框(就像您已经完成的一样)

dojo.query('.test').onclick(myFunc);

But, while adding new rows, it is your duty to attach this function to the newly added text boxes. 但是,在添加新行时,您有责任将此功能附加到新添加的文本框中。 You can refer it by the name "myFunc". 您可以通过名称“ myFunc”来引用它。

Note:- Running the code piece again - "dojo.query('.test').onclick(myFunc);" 注意:-再次运行代码段-“ dojo.query('。test')。onclick(myFunc);” will add duplicate handlers for existing textboxes. 将为现有文本框添加重复的处理程序。

If you are adding a large number of handlers or if the information on the screen is dynamic, you'll want to look at using delegated event handlers. 如果要添加大量的处理程序, 或者屏幕上的信息是动态的,则需要使用委托事件处理程序。

With delegation, you are only adding one handler, which is better for memory usage and you would be adding the handler to the container, not each specific item, which means that the items themselves can safely change without having to re-attach handlers to them. 使用委派,您只会添加一个处理程序,这对内存使用更好, 并且您会将处理程序添加到容器中,而不是将每个特定项添加到容器中,这意味着这些项本身可以安全地更改,而不必将处理程序重新附加到它们上。 。

For more details, I would point you towards this excellent answer: What is DOM Event delegation? 有关更多详细信息,我将为您提供一个出色的答案: 什么是DOM事件委托?

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

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