动态添加文本框,动态选择器不绑定.帮助! [英] Dynamically add textbox, dynamic selector not binding. Help!

查看:114
本文介绍了动态添加文本框,动态选择器不绑定.帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery拾色器-动态选择器中的代码.我做了一个按钮,它在表中动态创建了一行.在其中一个字段中,我希望它上面有jQuery Color Picker.

我知道通过文本框的类绑定jQuery是可行的.我在一行中放了一个带有颜色"作为其类的文本框.当我动态创建一行时,它不会绑定到该行的文本框.我做错什么了吗?

以下是我的代码中动态创建文本框的部分:

Hi I am using code from jQuery Colour Picker - Dynamic Selector. I have made a button where it dynamically creates a row into my table. In one of the fields I want it to have the jQuery Colour Picker on it.

I know that binding the jquery through the textbox''s class works. I put a row and put a textbox that has "color" as it''s class. When I dynamically create a row, it does not bind to the row''s textbox. Is there something wrong I am doing?

The following is a section of my code where I dynamically create a textbox:

// Add the textBox which is binded to a color picker
        var color = document.createElement('td');
        var textBox = document.createElement('input');
        textBox.setAttribute('id', tag + 'color');
        textBox.setAttribute('type', 'text');
        textBox.onblur = function () {
            updateColor(tag + 'color');
        };
        textBox.setAttribute("class","color"); // This is where the problem is.
        color.appendChild(textBox);
        tr.appendChild(color);

推荐答案

那是因为绑定到元素的事件不适用于动态添加的相似类型的元素.使用jQuery的.live绑定到创建的元素,即使您最初绑定到事件也是如此.

看看这个SO问题,发布者在将插件绑定到动态创建的元素上有类似的要求-
这是我的另一个答案,它解释了jQuery的实时功能-
在HTML表中插入输入类型按钮 [ http://stackoverflow.com/a/1317427/312219 [
That''s because events bound to an element does not apply to dynamically added similar type of elements. Use jQuery''s .live to bind to elements created even after you bind to events initially.

Check out this SO question, where the poster has a similar requirement to bind a plug-in to dynamically created elements - http://stackoverflow.com/questions/5766086/using-jquery-plugins-with-live[^] (Because applying plugins is not similar to binding to events using ''live'')

Here is another answer of mine which explains jQuery''s live - Insert Input type button in HTML Table[^]

Another option is to re-bind every time you create a new text box. Check out this link - http://stackoverflow.com/a/1317427/312219[^]

Hope this helps!


这篇关于动态添加文本框,动态选择器不绑定.帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆