Javascript 使用Jquery在循环内动态相乘值

Javascript 使用Jquery在循环内动态相乘值,javascript,php,jquery,Javascript,Php,Jquery,我有1个输入类型的字段…如果我更改了值,它将乘以循环中的值..以下是示例 <input type='number' id='values' name='values' onkeyup='multiplied()'> <?php foreach($data as $value) { $no = 1; echo "<input type='number' class='form-control value' id='value' name='

我有1个输入类型的字段…如果我更改了值,它将乘以循环中的值..以下是示例

<input type='number' id='values' name='values' onkeyup='multiplied()'>
<?php 
   foreach($data as $value)
   {
     $no = 1;
     echo "<input type='number' class='form-control value' id='value' name='value' readonly value=".$value['value'].">";
     echo "<input type='number' class='form-control' id='total' name='total' readonly >";
     $no++;
   }

?>

function multiplied()
{
  var values = $('#values').val();
  var value = $('.value').val();
  total = values * value;
  $('#total').val(total)
}


您可以在
函数上使用jquery

$(document).on('change', '#values', function() {
        var values = $("#values").val();            
        $(".value").each(function(index, element){
            values *= element.value;                 
            $(this).next('input#total').val(values);
        });
});

不要多次使用同一个id。改用类。此外,当您必须更改值时,还必须循环元素

改变

echo "<input type='number' class='form-control' id='total' name='total' readonly >";

如果您发布生成的HTML而不是PHP,这将非常有用,也更容易提供帮助!将
这个
包装回jQuery构造函数只是为了得到一个值?改用
this.value
。@RokoC.Buljan我已经编辑了我的代码,是你的意思吗?我可以把代码缩短一行。
echo "<input type='number' class='form-control total' name='total' readonly >";
<script>
function multiplied()
{
  var values = $('#values').val();
  $( ".value" ).each(function() {
      var value = $(this).val();
      total = values * value;
      $(this).next('input').val(total);
});

}
</script>
<script>
function multiplied()
{
  var values = $('#values').val();
  $( ".value" ).each(function() {
      var value = $(this).val();
      total = values * value;
      $(this).nextAll('.total').val(total);
});

}
</script>