gpt4 book ai didi

javascript计算器到小数位和图像作为按钮

转载 作者:行者123 更新时间:2023-12-02 19:36:32 25 4
gpt4 key购买 nike

我有一个小的 JavaScript 表单

<div id="calculator-text"><h2>Tape calculator - based on cable size 1 mm to 28 mm, with 15% overlap</h2></div>

<form name="form1" method="post" action="">

<div id="calcformlabel"><label for="val2">Enter your cable size</label> (in mm)</div>
<div id="calcformtext1"><input type="text" name="val2" id="val2"></div>

<div id="calcformbutton"><input type="button" name="calculate" id="calculate" value="Calculate"></div>

<div id="calcformresult">The tape size you require is:- <span id="result1" class="maintext1"></span> (mm)</div>


</form>
<script type="text/javascript">
var btn = document.getElementById('calculate');
btn.onclick = function() {
// get the input values
var val2 = parseInt(document.getElementById('val2').value);

// get the elements to hold the results
var result1 = document.getElementById('result1');

// create an empty array to hold error messages
var msg = [];
// check each input value, and add an error message
// to the array if it's not a number
if (isNaN(val2)) {
msg.push('<span class="maintext1">Enter your cable size</span>');
}
// if the array contains any values, display the error message(s)
// as a comma-separated string in the first <span> element
if (msg.length > 0) {
result1.innerHTML = msg.join(', ');
} else {
// otherwise display the results in the <span> elements
result1.innerHTML = val2 * 3.142 * 1.15;
}
};
</script>

基本上这是一个简单的计算

a)我怎样才能让它输出到小数点后两位(显然向上或向下舍入取决于 -.5 = 向下舍入和 +.5 = 向上舍入)

b)替换图像的输入类型按钮(我已经尝试过明显的代码和>输入类型=图像>,基本上这些确实有效,但不是显示实际结果,而是瞬间显示结果再次重新加载带有空白表单的页面...

对此的任何帮助将不胜感激

提前致谢

最佳答案

您问题的一部分

您可以通过以下方式将 JavaScript 舍入到特定精度

Link :Number rounding in JavaScript

var 原始=28.453

1) //round "original" to two decimals
var result=Math.round(original*100)/100 //returns 28.45


2) // round "original" to 1 decimal
var result=Math.round(original*10)/10 //returns 28.5


3) //round 8.111111 to 3 decimals
var result=Math.round(8.111111*1000)/1000 //returns 8.111

关于javascript计算器到小数位和图像作为按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com