简体   繁体   English

JQuery:对新添加的表行使用绑定

[英]JQuery : using bind for newly added table rows

I have a HTML table with one of the columns being a textbox column.我有一个 HTML 表,其中一列是文本框列。

I have set up a bind on the textbox when ever the textbox value is changed through the UI an attribute is added/updated as follows:当通过 UI 更改文本框值时,我在文本框上设置了一个绑定,如下所示添加/更新属性:

$('#container').find('.tableClass tbody').find('input[type=text]').bind('input', function () {
   $(this).attr('data-val', this.value);                
            });

This works fine for rows loaded on form load.这适用于表单加载时加载的行。 But when I add new a row to the table it does not seem to work.但是当我向表中添加新行时,它似乎不起作用。 I tried using something like below to utilize event delegation but this does not work.我尝试使用类似下面的东西来利用事件委托,但这不起作用。

$('#container').find('.tableClass tbody').on('bind', 'input[type=text] input', function () {
    $(this).attr('data-val', this.value);                
        });

Any ideas for how to get this to work for newly added rows?有关如何使其适用于新添加的行的任何想法?

You're calling .on() incorrectly.您错误地调用了.on() The first argument should be the event name (just like the first argument to .bind() ), the second argument should be the selector.第一个参数应该是事件名称(就像.bind()的第一个参数一样),第二个参数应该是选择器。 You used bind where the event belongs, and then you put the event name ( input ) into the selector.您在事件所属的地方使用了bind ,然后将事件名称 ( input ) 放入选择器中。 It should be:它应该是:

$('#container').find('.tableClass tbody').on('input', 'input[type=text]', function () {
    $(this).attr('data-val', this.value);                
});

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

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