Javascript 所有HTML字段值的总和不起作用
我在一个表单中有近100个HTML下拉列表,在提交之前,我想计算所有下拉框中所选值的总和。目前,它不起作用 jQuery:Javascript 所有HTML字段值的总和不起作用,javascript,jquery,html,blade,Javascript,Jquery,Html,Blade,我在一个表单中有近100个HTML下拉列表,在提交之前,我想计算所有下拉框中所选值的总和。目前,它不起作用 jQuery: <script type="text/javascript"> $(function () { var fields = $('#addFullAuditReport :input').change(calculate); function calculate() { var score = 0;
<script type="text/javascript">
$(function () {
var fields = $('#addFullAuditReport :input').change(calculate);
function calculate() {
var score = 0;
fields.each(function () {
score += +$(this).val();
})
$('#price').html(score.toFixed(2));
}
})
</script>
{!! Form::open(array('route' => array('addFullHandHygieneAudit'),
'role'=>'form','id'=>'addFullHandHygieneAudit', 'class'=>'form-horizontal')) !!}
我的问题是没有任何东西添加到HTML输出中。jQuery可能是正确的,但在将其添加到字段之前,我无法验证它
如果您有任何帮助,我们将不胜感激。看起来您有打字错误,请更改
$('#price').html(price.toFixed(2));
到
看起来你有打字错误,改变一下
$('#price').html(price.toFixed(2));
到
你应该试试这样的东西
$(function () {
var fields = $('select').change(calculate);
function calculate() {
var score = 0;
fields.each(function () {
var val = $(this).val();
score += isNaN(val) ? 0 : parseFloat(val);
})
$('#price').html(score.toFixed(2));
}
calculate();
})
你应该试试这样的东西
$(function () {
var fields = $('select').change(calculate);
function calculate() {
var score = 0;
fields.each(function () {
var val = $(this).val();
score += isNaN(val) ? 0 : parseFloat(val);
})
$('#price').html(score.toFixed(2));
}
calculate();
})
没有
输入
元素出现在表单中
;将选择器更改为parentElement#input
,例如<代码>$(“.form group#input”)。另外,用score.toFixed(2)
代替price.toFixed(2)
$(函数(){
变量字段=$(“.form group#input”).change(计算);
函数计算(){
var得分=0;
字段。每个(函数(){
分数+=+$(this.val();
})
$('#price').html(score.toFixed(2));
}
})
1.4
不
对
总计:
无输入
元素出现在表格中
;将选择器更改为parentElement#input
,例如<代码>$(“.form group#input”)。另外,用score.toFixed(2)
代替price.toFixed(2)
$(函数(){
变量字段=$(“.form group#input”).change(计算);
函数计算(){
var得分=0;
字段。每个(函数(){
分数+=+$(this.val();
})
$('#price').html(score.toFixed(2));
}
})
1.4
不
对
总计:
我认为我们不能像这样使用更改事件处理程序。我的建议是:
$(function ()
{
$('#addFullAuditReport :input').change(function()
{
var fields = $('#addFullAuditReport :input');
var score = 0;
fields.each(function() {
score += $(this).val();
});
$('#price').html(score.toFixed(2));
}
});
我认为我们不能像这样使用变更事件处理程序。我的建议是:
$(function ()
{
$('#addFullAuditReport :input').change(function()
{
var fields = $('#addFullAuditReport :input');
var score = 0;
fields.each(function() {
score += $(this).val();
});
$('#price').html(score.toFixed(2));
}
});
抱歉,我也注意到了。我已经修改了这个,但是这个问题仍然很明显,这个领域没有更新。抱歉,我也注意到了。我已经修改了这个,这个问题仍然很明显,这个字段没有更新。谢谢你的回复。此代码在控制台中显示此错误-[error]TypeError:score.toFixed不是函数。(在'score.toFixed(2)'中,'score.toFixed'未定义)。感谢您的回复。此代码在控制台中显示此错误-[error]TypeError:score.toFixed不是函数。(在“score.toFixed(2)”中,“score.toFixed”未定义)。