简体   繁体   English

如何让 jAutoCalc 处理动态添加的元素

[英]How to make jAutoCalc work on dynamically Added elements

Let me explain what i'm doing : i have a Orders table where i can add, delete and edit the client's orders.让我解释一下我在做什么:我有一个订单表,我可以在其中添加、删除和编辑客户的订单。 Everything works fine for the add and delete.添加和删​​除一切正常。 But when it comes to edit i have some trouble.但是当涉及到编辑时,我遇到了一些麻烦。 What i'm doing is pulling data from the DATABASE (using codeigniter) and displaying them to the edit modal so that i can then edit them if the clients wishes so.我正在做的是从数据库中提取数据(使用 codeigniter)并将它们显示到编辑模式,以便我可以在客户希望的情况下编辑它们。 The Client's infos(name,adresse,state..etc) load fine but when i load his orders with the products and dynamically add them using (jQuery, ajax), the Item total field ( which calculates the sum of the product orders * its unit price using the jAutoCalc plugin) doesn't seems to work.客户的信息(名称、地址、状态..等)加载正常但是当我用产品加载他的订单并使用(jQuery、ajax)动态添加它们时,项目总计字段(计算产品订单的总和 * 其使用 jAutoCalc 插件的单价)似乎不起作用。 The plugin works just fine when i'm not adding dynamically the data.当我不动态添加数据时,该插件工作正常。 Is there a way to fix this or add something that will make it work?有没有办法解决这个问题或添加一些使它工作的东西? here's my code这是我的代码

var thehtml = ''
for(i=0; i<data[1].length; i++){
    thehtml += 
              '<tr class=cmd'+id+'>'+
                 '<td>'+
                    '<select name="products" id="productselect>"'+
                        '<option data-price="39" value="1">Knife</option>'+
                        '<option data-price="3" value=2">Bottle</option>'+
                        '<option data-price="4" value="3">Pen</option>'+
                    '</select>'+
                 '</td>'+
                 '<td><input type="text" name="qty" value="39"></td>'+
                 '<td><input type="text" name="price" value="33"></td>'+
                 '<td><input readonly type="text" jAutoCalc="{qty} * {price}"/>'+ /// THIS DOESNT WORK IT'S DYNAMICALLY ADDED !!!!
                 '<button type="button" name="add" value="Add" class="tr_clone_remove  btn btn-danger">Remove</button>'+'</td>'+
              '</tr>;';
$(thehtml).insertAfter(itemselement); 

Please note that everything works find in non dynamically added elements.请注意,一切都可以在非动态添加的元素中找到。 But whenever i added these table dynamically nothing works !但是每当我动态添加这些表时,什么都不起作用! please help me fix it !请帮我修复它!

When you're adding elements to the DOM after the jAutoCalc has already been initialized, the plugin is not aware that there are new elements.当您在jAutoCalc已经初始化后向 DOM 添加元素时,插件不知道有新元素。 You need to initialize the jAutoCalc on these elements.您需要在这些元素上初始化jAutoCalc

You could simply do something like this你可以简单地做这样的事情

$(thehtml).jAutoCalc();

after you appended the new rows.追加新行后。

If that doesn't work, try如果这不起作用,请尝试

$('form').jAutoCalc();

It all depends on your HTML structure but you just need to reinitialize it.这完全取决于您的 HTML 结构,但您只需要重新初始化它。

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

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