计算输入值 [英] Count input values

查看:102
本文介绍了计算输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不刷新页面的情况下计算插入表单的值.如果所有输入的"name ='test'"设置相同,则以下代码可以很好地工作,但是一旦使用php提交表单,我需要使用唯一的名称来处理表单.

I would like to count values inserted into a form without refreshing the page. The below code works well if all of the inputs have "name='test'" set the same but I need name to be unique to process the form once its submitted using php.

有人知道我如何在JavaScript/jquery中使用"id ='count'"获得输入值吗?

Does anyone know how I can get the input values using "id='count'" with javascript/jquery?

在此先感谢您的帮助.

Thanks in advance for your help.

例如

function findall() {
  var array = document.getElementsByName('price_no_vat');
  var total = 0;
  for (var i = 0; i < array.length; i++) {
    if (parseInt(array[i].value))
      total += parseInt(array[i].value);
  }
  document.getElementById('3').value = total;
}

<input id="1" name="price_no_vat" onblur="findall()">
<input id="2" name="VAT" onblur="findall()">
<input id="3" mame="all" disabled>

推荐答案

我不了解逻辑.简而言之,您可以执行以下操作:

I don't understand the logic. In simple words, you can do:

$(function () {
  $("input").blur(function () {
    $("#3").val(function () {
      var i1 = parseFloat($("#1").val());
      var i2 = parseFloat($("#2").val());
      i1 = isNaN(i1) ? 0 : i1;
      i2 = isNaN(i2) ? 0 : i2;
      return i1 + i2;
    });
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="1" name="price_no_vat" />
<input id="2" name="VAT" />
<input id="3" mame="all" disabled />

使用纯JavaScript,您可以实现以下目标:

Using pure JavaScript, you can achieve this:

function recalc() {
  var i1 = parseFloat(document.getElementById("1").value);
  var i2 = parseFloat(document.getElementById("2").value);
  i1 = isNaN(i1) ? 0 : i1;
  i2 = isNaN(i2) ? 0 : i2;
  document.getElementById("3").value = i1 + i2;
}

<input id="1" name="price_no_vat" onblur="recalc()" />
<input id="2" name="VAT" onblur="recalc()" />
<input id="3" mame="all" disabled />

这篇关于计算输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆