Javascript 增加输入结果并包含逗号

标签 javascript jquery forms

我似乎无法正常工作,这让我发疯。我制作了一个 JSFiddle 并且热衷于让任何知识比我多的人来看看! http://jsfiddle.net/kLQf3/9/

基本上我想做的是让用户能够在第一个“收件人”字段中输入数字(包括逗号),即。 “60,000”,我希望这个数字显示在“发件人”字段中,它增加 1,即。 “60,001”。

输入框也一样。我对 JS 的了解有限,但我的自动更新工作正常,但增量不断给我 NaN 错误,因为包含逗号。

window.onload = function () {
  var first = document.getElementById('firstFieldId'),
      second = document.getElementById('secondFieldId'),
      third = document.getElementById('thirdFieldId'),
      fourth = document.getElementById('fourthFieldId');

  first.onchange = function () {
    second.value = first.value;
  third.onchange = function () {
    fourth.value = third.value;
};
};
};

最佳答案

只需使用 parseint 或 pasefloat:

window.onload = function () {
var first = document.getElementById('firstFieldId'),
  second = document.getElementById('secondFieldId'),
  third = document.getElementById('thirdFieldId'),
  fourth = document.getElementById('fourthFieldId');

first.onchange = function () {
second.value =  parseFloat(first.value)+1;
alert(second.value);
third.onchange = function () {
fourth.value = parseFloat(third.value)+1;
};
};
};

关于Javascript 增加输入结果并包含逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21545834/

相关文章:

javascript - 如何在没有像素模糊的情况下调整 html5 Canvas 大小

javascript - 如果 URL 的链接返回错误,则禁用 HTML 链接

php - 如何简化 PHP 中的表单处理?

php - 通过在 laravel 中快速单击来防止多次提交表单

javascript - 使用 jquery 验证表单输入字段

javascript - 显示 jquery ui "instance"方法的键和值

javascript - Angular 谷歌地图

javascript - 在 .NET core MVC 应用程序中包含带 @ 符号的 Javascript 库

PHP 未接收到 JavaScript 值

javascript - 邮政编码到城市自动完成