首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将jQuery ()隔离到起始元素的父容器的子容器中

如何将jQuery ()隔离到起始元素的父容器的子容器中
EN

Stack Overflow用户
提问于 2013-04-03 23:36:33
回答 1查看 455关注 0票数 0

在下面的脚本中,我需要修改tab(),以便它不使用泛型类,而是从目标元素#customfield_11071开始,然后向上爬行到最近的“活动窗格”父元素(.active- jQuery #tab-5),并只将带有class="text.long-field“的子元素输入传递给each?

我认为通过直接将#tab-5传递给each可以做到这一点,但它会在tab-5父容器之外拾取与类匹配的文本输入字段,并将这些字段添加到总数中,从而导致错误的计算。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
  <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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-03 23:49:09

您的脚本在隔离状态下运行良好。参见http://jsfiddle.net/jEEKL/。如果您使用此jQuery("#tab-5 input.text.long-field") jQuery选择器获取额外的字段,请检查您的源中是否有重复的选项卡ids。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15791768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档