Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.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-在输入中添加小数点_Javascript - Fatal编程技术网

Javascript-在输入中添加小数点

Javascript-在输入中添加小数点,javascript,Javascript,我得到了一个脚本的帮助,但我需要编辑它才能正常工作 我是0级新手,在JS中,有超过我的分数 脚本的原始版本允许我添加值​​但问题是,它没有为数千个输入添加小数点示例:2.500,只显示2500个,没有小数点 我还必须为求和的结果保留本地存储,这里的一切对我来说都很糟糕 我有一个 你能指导我更好地实现脚本的新部分吗 注意:添加在脚本的末尾 提前谢谢 //-------------- 脚本: 对于总输入,必须将ToLocalString添加到displaySuma 谢谢HVD。。。它的工作非常出色,

我得到了一个脚本的帮助,但我需要编辑它才能正常工作

我是0级新手,在JS中,有超过我的分数

脚本的原始版本允许我添加值​​但问题是,它没有为数千个输入添加小数点示例:2.500,只显示2500个,没有小数点

我还必须为求和的结果保留本地存储,这里的一切对我来说都很糟糕

我有一个

你能指导我更好地实现脚本的新部分吗

注意:添加在脚本的末尾

提前谢谢

//--------------

脚本:


对于总输入,必须将ToLocalString添加到displaySuma

谢谢HVD。。。它的工作非常出色,只需一行脚本。。。美丽的
let clicks = 0;
let clicksdos = 0;

const safeInt = (key) => {
  let value = parseInt(getValue(key));
  return (isNaN(value) || value < 0) ? 0 : value;
}

// This loads our clicks from the LocalStorage
const loadClicks = () => {
  clicks = safeInt('clicks');
  clicksdos = safeInt('clicksdos');
}

const loadHTML = () => {
  return getValue('html', '');
}

const loadFromStorage = () => {
  let html = loadHTML();
  if (html !== '') {
    loadClicks();
  }
  displayClicks();
  document.querySelector(".derecha").innerHTML = html;
}

// Display the clicks on the screen
const displayClicks = () => {
  clicks = (clicks === NaN) ? 0 : clicks;
  clicksdos = (clicksdos === NaN) ? 0 : clicksdos;
  document.querySelector('#clicks').innerHTML = clicks;
  document.querySelector('#clicksdos').innerHTML = clicksdos;
  // Hide / Show Result 
  let display = (clicks > 0) ? 'block' : 'none';
  document.querySelector('#cont-resultado').style.display = display;
}

const adjustClicks = (value) => {
  clicks += value;
  clicksdos += value;
  storeValue('clicks', clicks);
  storeValue('clicksdos', clicksdos);
  displayClicks();
}
const addClick = () => adjustClicks(1);
const removeClick = () => adjustClicks(-1);


// Manage localStorage
const storeValue = (key, value) => (localStorage) ? localStorage.setItem(key, value) : '';
const getValue = (key, defaultValue) => (localStorage) ? localStorage.getItem(key) : defaultValue;
const storeHTML = () => storeValue("html", document.getElementsByClassName("derecha")[0].innerHTML);

// Add a node to the Derecha
const addToDerecha = (nodeId) => {
  let node = document.querySelector(`#${nodeId}`);
  document.querySelector('.derecha').appendChild(node.cloneNode(true));
  storeHTML();
  displaySuma();
};

// Monitor ALL click events 
document.addEventListener('click', (event) => {
  let target = event.target;
  // Add
  if (target.matches('.comp-clone')) {
    addClick();
    addToDerecha(event.target.dataset.clone);
  }
  // Remove
  if (target.matches('.bbp')) {
    removeClick();
    getParent('.derecha', target).removeChild(target.parentNode);
    storeHTML();
    displaySuma();
  }
});

// This is just a helper function.
const getParent = (match, node) => (node.matches(match)) ? node : getParent(match, node.parentNode);

// New Script for sum inputs
const displaySuma = () => document.getElementById("total").value = suma();

const suma = function() {
  return Array.from(document.querySelectorAll(".derecha div .add-prod"))
    .reduce((a, v) => a + parseFloat(v.value), 0);
}

// Code to run when the document loads.
document.addEventListener('DOMContentLoaded', () => {
  if (localStorage) {
    loadFromStorage();
  }

  displaySuma();

// ========== Start - NEW - Add Decimal Point to input #total ==========

/*Reference to the inputs that have the add-prod class to then add each value of those inputs */
var toAdd = document.querySelectorAll('.add-prod');

/* Reference to the input of the total */
var ibxTotal=document.getElementById('total');

/* We create a subTotal variable initialized to 0 */
var subTotal = 0;

/*Here the inputs are traversed with add-prod, accumulating their values in subTotal*/
toAdd.forEach(function(item) {
  subTotal += parseInt(item.value);
});

 /* We create a variable with the cumulative value in subTotal formatted to the 'es-ES' style*/

var total = new Intl.NumberFormat('es-ES').format(subTotal);
console.log(total);

/* We put that value in the input of the total */
ibxTotal.value=total;

// ========== End - NEW - Add Decimal Point to input #total ==========

});
const displaySuma = () => document.getElementById("total").value = suma().toLocaleString( "es-ES" );