在下面的脚本中,我需要修改tab(),以便它不使用泛型类,而是从目标元素#customfield_11071开始,然后向上爬行到最近的“活动窗格”父元素(.active- jQuery #tab-5),并只将带有class="text.long-field“的子元素输入传递给each?
我认为通过直接将#tab-5传递给each可以做到这一点,但它会在tab-5父容器之外拾取与类匹配的文本输入字段,并将这些字段添加到总数中,从而导致错误的计算。
jQuery(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
jQuery("#tab-5 input.text.long-field").each(function() {
jQuery(this).keyup(function(){
calculateSum();
});
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
jQuery("#tab-5 input.text.long-field").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0 && this.id !== "customfield_11071") {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
jQuery("#customfield_11071").val(sum.toFixed(2));
}HTML
<div class="tabs-pane active-pane" id="tab-5">
<div class="field-group">
<label for="customfield_12370">
Test Case Estimate
</label>
<input class="text long-field" id="customfield_12370" name="customfield_12370" type="text" value="">
<div class="description">
Will hold all QA estimates for Test Case preparation/creation efforts.
</div>
</div>
<div class="field-group">
<label for="customfield_12371">
Test Analysis Estimate
</label>
<input class="text long-field" id="customfield_12371" name="customfield_12371" type="text" value="">
<div class="description">
Will hold all QA estimates for testing analysis efforts.
</div>
</div>
<div class="field-group">
<label for="customfield_11071">
QA Estimate Total
</label>
<input class="text long-field" id="customfield_11071" name="customfield_11071" type="text" value="">
<div class="description">
Estimated LOE in Hours
</div>
</div>
</div>更新:或者,我可以将特定的I集合填充到一个数组中,并将其传递给each(),而不是将类字符串传递给each?
发布于 2013-04-03 23:49:09
您的脚本在隔离状态下运行良好。参见http://jsfiddle.net/jEEKL/。如果您使用此jQuery("#tab-5 input.text.long-field") jQuery选择器获取额外的字段,请检查您的源中是否有重复的选项卡ids。
https://stackoverflow.com/questions/15791768
复制相似问题