JS send value to input hidden HTML not work

2019-10-09 02:26发布

问题:

I have a very big problem. This is my code:

function calcolaGiorni() {
	var data1 = document.getElementById('set').value;
	var data2 = document.getElementById('set1').value;
	var diffTempo = Math.abs(data2.getTime() - data1.getTime());
	var diffGiorni = Math.ceil(diffTempo / (1000 * 3600 * 24));
	var prezzo = 500;
	var totale = diffGiorni*prezzo;
	document.getElementById('stampaTot').value = totale;
}
<input type="hidden" name="prezzoTotale" id="stampaTot">

The hidden input will contain the operation result, but anything don't work.

回答1:

Try stepping through your code in the browser debugger. Verify that your variables are numbers and can result in valid results.

Check in the console (ctrl+shift+j in Chrome) to see the results, or use an alert box, as below.

function calcolaGiorni() {
var data1 = document.getElementById('set').value;
var data2 = document.getElementById('set1').value;
var diffTempo = Math.abs(data2.getTime() - data1.getTime());
var diffGiorni = Math.ceil(diffTempo / (1000 * 3600 * 24));
var prezzo = 500;
var totale = diffGiorni*prezzo;
//check variable values for anything suspect based on the original inputs
alert(totale) //shows an alert box for the value totale
console.log(totale) //view the variable in the console 
document.getElementById('stampaTot').value = totale;
}