我想制作一个表格,其中有 7 个输入字段,我在其中输入数字,最后一个输入字段,其中所有插入的数字总和为一个结果。我试图从其他堆栈器编辑一些脚本,但由于某种原因它不显示结果。
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>
到目前为止,我的脚本如下所示:
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);
});
这是它的jsfiddle:http: //jsfiddle.net/bT4nm/1/
你可以帮帮我吗?是我的 html 类中的问题还是脚本中的问题我对 jQuery 很陌生,我需要尽快解决。更新我的 js fiddle 会很棒