Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 所有HTML字段值的总和不起作用_Javascript_Jquery_Html_Blade - Fatal编程技术网

Javascript 所有HTML字段值的总和不起作用

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;

我在一个表单中有近100个HTML下拉列表,在提交之前,我想计算所有下拉框中所选值的总和。目前,它不起作用

jQuery:

  <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”未定义)。