javascript jquery 将多个输入字段合二为一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16956444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery sum of multiple input fields in one
提问by dzordz
I wanna make form in which I have 7 input fields in which I'm entering numbers and one last input fields where all inserted numbers are summed in one result. I've tried to edit some script from other stacker but from some reason it doesn't display the result.
我想制作一种表格,其中有 7 个输入字段,我在其中输入数字,最后一个输入字段将所有插入的数字汇总为一个结果。我试图从其他堆栈器编辑一些脚本,但由于某种原因它没有显示结果。
the html is:
html是:
<form class="form-horizontal" id="whereEntry" method='post' action=''>
<fieldset>
<input type="text" class="income_count span1 register_input" id="income" name="income" placeholder="% of income"> <br>
<input type="text" class="income_count span1 register_input" id="income_2" name="income_2" placeholder="% of income"> <br>
<input type="text" class="income_count span1 register_input" id="income_3" name="income_3" placeholder="% of income"> <br>
<input type="text" class="income_count span1 register_input" id="income_4" name="income_4" placeholder="% of income"> <br>
<input type="text" class="income_count span1 register_input" id="income_5" name="income_5" placeholder="% of income"> <br>
<input type="text" class="income_count span1 register_input" id="income_6" name="income_6" placeholder="% of income"> <br><br><br>
<input type="text" class="span2 register_input" id="income_sum" name="income_sum" placeholder="% of income"> <br>
</fieldset>
</form>
and my script so far looks like this:
到目前为止,我的脚本如下所示:
var $form = $('#whereEntry'),
$summands = $form.find('.income_count'),
$sumDisplay = $('#income_sum');
$form.delegate('.income_count', 'change', function ()
{
var sum = 0;
$summands.each(function ()
{
var value = Number($(this).val());
if (!isNaN(value)) sum += value;
});
$sumDisplay.text(sum);
});
here is jsfiddle of it: http://jsfiddle.net/bT4nm/1/
这是它的 jsfiddle:http: //jsfiddle.net/bT4nm/1/
could you help me? is the problem in my html classes or something in the script I'm nooby about jQuery and i need as ASAP solution.. update of my js fiddle would be great
你可以帮帮我吗?是我的 html 类中的问题还是脚本中的问题我对 jQuery 不感兴趣,我需要尽快解决方案..更新我的 js 小提琴会很棒
采纳答案by bugwheels94
回答by bugwheels94
Use this:
用这个:
var $form = $('#whereEntry'),
$summands = $form.find('.income_count'),
$sumDisplay = $('#income_sum');
$form.delegate('.income_count', 'change', function ()
{
var sum = 0;
$summands.each(function ()
{
var value = Number($(this).val());
if (!isNaN(value)) sum += value;
});
$sumDisplay.val(sum);
});