使用jQuery和Drupal6自动更新宽度/长度/区域

我正在使用Drupal创建一个测量表( Drupal 6中的测量表 ),并且我已经准备好所有字段,我只需要编写一些jQuery,当用户输入数据时自动更新相应的字段。 代码如下:

jQuery(document).ready( function ($) { $('input').change( function() { $parent = $(this).parent("td").children("div").children("div"); length = $parent.children("div[id*='field-length-0-wrapper']").children("div.container-inline").children('div[id*="field-length-0-value-wrapper"]').children('input[id*="field-length-0-value"]').val(); // get the width width = $parent.children("div[id*='field-width-0-wrapper']").children("div.container-inline").children('div[id*="field-width-0-value-wrapper"]').children('input[id*="field-width-0-value"]').val(); // get the length $parent.children("div[id*='field-area-0-wrapper']").children("div.container-inline").children('div[id*="field-area-0-value-wrapper"]').children('input[id*="field-area-0-value"]').val(length * width); //put area } ); }); 

这与以下表单代码交互:

 Measurements:    
 
---mftin
---mftin
---ft2m2
-2-1012
 
---mftin
---mftin
---ft2m2
-2-1012

这会显示两个房间块 – 请注意field_room []值。 基本上我所做的是创建两个CCK内容类型 – Sheet和Room。 房间有四个文本字段:名称,宽度,长度,面积 – 按此顺序。 名称是自动完成字段,宽度,长度和面积是测量值字段,表示可以切换单位。 然后,在Sheet内容类型中,我使用flexinode创建一系列重复的房间内容类型。

我的jQuery有什么问题?

任何帮助表示赞赏。 🙂

在您的jQuery代码中,应该使用parent代替parent进行td查找:

 $parent = $(this).parents("td").children("div").children("div"); 

这使得一旦宽度或长度改变就计算面积值。

编辑:

还有可能优化jQuery,例如对此(可以进一步改进):

 jQuery(document).ready( function ($) { $('input').change(function() { var $parent = $(this).parents("td").children("div").children("div"), length = $parent.find('input[id*="field-length-0-value"]').val(), width = $parent.find('input[id*="field-width-0-value"]').val(); $parent.find('input[id*="field-area-0-value"]').val(length * width); }); });