2011-12-08 69 views
1

我需要显示每个输入值包含在完整数据集之外的百分比。计算动态输入字段数量之间的百分比

因此,如果用户输入3,2和1的“百分比” inputfield应显示“50,30和20”

的事情是,我不知道该表将有多少投入得到。它可以从1到任意数量。

源html代码是由我创建的局部视图创建的。输出是:

<table> 
    <tr> 
     <th>Name</th><th>Distributiob key</th><th>Percent</th> 
    </tr> 

<tr> 
    <td> 
     House 1 
    </td> 
    <td> 
     <input type="text" size="5" value="0" id="LightPhysicalEntities_0__DistributionKey" name="LightPhysicalEntities[0].DistributionKey" /> 
    </td> 
    <td> 
     <input type="text" size="5" id="LightPhysicalEntities_0__Percent" readonly=readonly /> 
    </td> 
</tr> 
<tr> 
    <td> 
     House 2 
    </td> 
    <td> 
     <input type="text" size="5" value="0" id="LightPhysicalEntities_1__DistributionKey" name="LightPhysicalEntities[1].DistributionKey" /> 
    </td> 
    <td> 
     <input type="text" size="5" id="LightPhysicalEntities_1__Percent" readonly=readonly /> 
    </td> 
</tr> 
<tr> 
    <td> 
     House 3 
    </td> 
    <td> 
     <input type="text" size="5" value="0" id="LightPhysicalEntities_2__DistributionKey" name="LightPhysicalEntities[2].DistributionKey" /> 
    </td> 
    <td> 
     <input type="text" size="5" id="LightPhysicalEntities_2__Percent" readonly=readonly /> 
    </td> 
</tr> 

表中的第一个字段是标题或名称。其次是用户在分发号码中打出的位置。第三个是只有已计算百分比的字段。

这个计算应该用javascript来完成,但是我不知道如何开始,获取输入和设置输出。

+0

* ...如果用户输入3,2和1的 “百分比” inputfield应显示“50,30和20”*基于什么? –

+0

@mfiis与David一起 - 这些值是如何计算的? –

+0

对不起,我错过了那部分。 3 + 2 + 1 = 6.3是6的50%。2是6的30%,1是6的20%。 有意义吗? – mfriis

回答

3

这样的功能应该帮助(这将四舍五入的percentages-更换Math.floor如果要处理不同):

function calcPcts() { 
    var distributions = $("[id$=DistributionKey]"); 

    // Get the base first 
    var base = 0; 
    distributions.each(function() { 
     base += window.parseInt($(this).val(), 10); 
    }); 

    // Now calculate the percentages of the individual ones 
    distributions.each(function() { 
     var input = $(this), 
      val = window.parseInt($(this).val(), 10), 
      pct = (val === 0) ? val : Math.floor((val/base) * 100); 
     $("#" + input.attr("id").replace("DistributionKey", "Percent")).val(pct); 
    }); 
} 
+0

这个伎俩!谢谢。我想到的是:http://pastebin.com/0UjZM97L,但我没有时间让它工作。 你能解释一下你在函数的“最后”行做什么吗?我不太确定那里发生了什么。 – mfriis

+0

很高兴帮助:-)它所做的是为它正在处理的当前分配键“输入”选择百分比“输入”。对于''LightPhysicalEntities_0__DistributionKey'',它用''Percent''替换''DistributionKey''到达''LightPhysicalEntities_0__Percent''。然后它将它和'“#”'一起使用来通过ID进行选择。一旦它获得了元素,它就会将它的'value'属性(通过'val()')设置为它在上面计算的百分比。 – jabclab

2

如果你添加一个类的输入字段,你可以使用jQuery获得该类别的项目数量。然后您可以获取这些值并执行您的计算。

下面是一个例子:

var inputCount = $(".inputfield").length; 

$("input[type='text']").change(function() { 
    var total = GetTotal(); 
    for (i = 0; i < inputCount; i++) { 
     var inputValue = $('#LightPhysicalEntities_' + i + '__DistributionKey').val(); 
     if (total != 0) { 
      var percent = inputValue/total; 
      $('#LightPhysicalEntities_' + i + '__Percent').val(percent); 
     } 
    } 
}); 

function GetTotal() { 
    var total = 0; 
    for (i = 0; i < inputCount; i++) { 
     var inputValue = $('#LightPhysicalEntities_' + i + '__DistributionKey').val(); 
     total += parseInt(inputValue); 
    } 
    return total; 
} 

这里是脚本工作的一个例子:http://jsfiddle.net/ZWuQr/