简体   繁体   中英

JS adding text after result

I have this code that gives an correct output like

document.getElementById('add_calc_gewicht').value = (((Math.PI * (document.getElementById('add_calc_dia_out').value / 2) * (document.getElementById('add_calc_dia_out').value / 2) / 1000) * (document.getElementById('add_breedte').value / 1000) * document.getElementById('add_calc_srt_gew').value) - ((Math.PI * (document.getElementById('add_calc_dia_inner').value / 2) * (document.getElementById('add_calc_dia_inner').value / 2) / 1000) * (document.getElementById('add_breedte').value / 1000) * document.getElementById('add_calc_srt_gew').value)).toFixed(0);

document.getElementById('add_calc_lengte').value = (document.getElementById('add_calc_gewicht').value / document.getElementById('add_breedte').value / document.getElementById('add_dikte').value / document.getElementById('add_calc_srt_gew').value * 1000000).toFixed(0);

But I would like to add kg to the weight and mm to the length. It works seperat but I can't get it to work both!

When adding mm to the length and not changing code for the weight I have this result:

在此处输入图片说明

document.getElementById('add_calc_gewicht').value = (((Math.PI * (document.getElementById('add_calc_dia_out').value / 2) * (document.getElementById('add_calc_dia_out').value / 2) / 1000) * (document.getElementById('add_breedte').value / 1000) * document.getElementById('add_calc_srt_gew').value) - ((Math.PI * (document.getElementById('add_calc_dia_inner').value / 2) * (document.getElementById('add_calc_dia_inner').value / 2) / 1000) * (document.getElementById('add_breedte').value / 1000) * document.getElementById('add_calc_srt_gew').value)).toFixed(0);

document.getElementById('add_calc_lengte').value = (document.getElementById('add_calc_gewicht').value / document.getElementById('add_breedte').value / document.getElementById('add_dikte').value / document.getElementById('add_calc_srt_gew').value * 1000000).toFixed(0) + ' mm';

When I change now the code for the weight I have this result

在此处输入图片说明

document.getElementById('add_calc_gewicht').value = (((Math.PI * (document.getElementById('add_calc_dia_out').value / 2) * (document.getElementById('add_calc_dia_out').value / 2) / 1000) * (document.getElementById('add_breedte').value / 1000) * document.getElementById('add_calc_srt_gew').value) - ((Math.PI * (document.getElementById('add_calc_dia_inner').value / 2) * (document.getElementById('add_calc_dia_inner').value / 2) / 1000) * (document.getElementById('add_breedte').value / 1000) * document.getElementById('add_calc_srt_gew').value)).toFixed(0) + ' kg';

document.getElementById('add_calc_lengte').value = (document.getElementById('add_calc_gewicht').value / document.getElementById('add_breedte').value / document.getElementById('add_dikte').value / document.getElementById('add_calc_srt_gew').value * 1000000).toFixed(0) + ' mm';

Any suggestions? (and explanation)

You added text to the numbers, now you can no longer calculate with them, resulting in NaN .

If you use parseInt (since you require Integers) as follows:

parseInt(document.getElementById('Weight and Length fields').value, 10)

they will be converted into Numbers again fixing your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM