我在 JavaScript 中将值四舍五入为 0.25、.50、.75 和 .00。代码如下。
function roundOff(obj) {
//alert(document.getElementById("sample").value);
var val=obj.value;
var newValue;
var floorValue = Math.floor(val);
var remainder = val - floorValue;
if (remainder < 0.325) {
if (remainder < 0.125) {
newValue = floorValue;
} else {
newValue = floorValue + 0.25;
}
} else {
if (remainder < 0.625) {
newValue = floorValue + 0.5;
} else if (remainder < 0.875) {
newValue = floorValue + 0.75;
} else {
newValue = floorValue + 1;
}
}
alert(floorValue);
alert(newValue);
document.getElementById("sample").value=newValue;
//return obj;
}
但我无法将值放回 jsp 页面。如果我输入 5.5,jsp 中只显示 5。但如果我发出警报并检查下限值(value)的值,那么它是 5.5。但如果我把它放到 jsp 中的文本框中,它只给出 5。为什么会这样???
最佳答案
您将文本框的值设置为floorValue,而不是newValue。
关于javascript - 无法在jsp中显示期望值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607147/