Javascript-在输入中添加小数点

时间:2019-04-22 02:06:58

标签: javascript

我正在获得有关脚本的帮助,但是我需要对其进行编辑以使其正常工作。

我是JS的新手(0级),有些要点超越了我...

脚本的原始版本允许我添加多个输入的值,但是问题是它没有为数千添加小数点(例如:2.500)并且仅显示2500(无点)。

我还必须保留LocalStorage作为求和的结果,这里一切对我来说都很糟糕。

我有一个 DEMO LIVE HERE

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

注意:添加位于脚本的结尾。

谢谢!

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

脚本:

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 ==========

});

1 个答案:

答案 0 :(得分:1)

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

class CustomNSMutableURLRequest: NSMutableURLRequest {

    convenience init(url : URL) {
        self.init(url: url)
        self.httpShouldHandleCookies = false
        self.httpMethod = "GET"
        print("Custom Request!")

    }
}