[英]How to correctly use values from textboxes in html pls help me
I wanted to use values from textboxes but failed so i tried with constant values and now I am getting a NAN error.我想使用文本框中的值但失败了,所以我尝试使用常量值,现在我收到了 NAN 错误。 I am showing my result in a label btw.
顺便说一句,我在标签中显示了我的结果。
function myFunction() {
var translength = 2400
var transSpacing = 150
var transEndOverhang = 75
var transStartOverhang = 75
var longLength = 6000
var LongSpacing = 150
var LongStartOverhang = 75
var LongEndOverhang = 75
if (transSpacing != 0)
document.getElementById('longAmount').value = ((transLength - transStartOverhang - transEndOverhang) / transSpacing) + 1;
document.getElementById('longAmount').innerHTML = document.getElementById('longAmount').value
if (document.getElementById('longAmount').value > 0 && transStartOverhang + ((document.getElementById('longAmount').value - 1) * transSpacing) + transEndOverhang < transLength)
document.getElementById('longAmount').value = longAmount + 1;
document.getElementById('longAmount').innerHTML = document.getElementById('longAmount').value
}
You're mixing innerHTML
and value
for the same id.您正在为相同的 id 混合
innerHTML
和value
。 If that id is a textbox you should use .value
.如果那个 id 是一个文本框,你应该使用
.value
。 Also, to convert strings (the text from the textarea) you can use parseInt()
or parseFloat()
.此外,要转换字符串(来自 textarea 的文本),您可以使用
parseInt()
或parseFloat()
。
// Here you're taking a bunch of variables and sets the textareas value
document.getElementById('longAmount').value = ((transLength - transStartOverhang - transEndOverhang) / transSpacing) + 1;
// Makes no sense (textarea doesn't have a innerHTML)
document.getElementById('longAmount').innerHTML = document.getElementById('longAmount').value;
// document.getElementById('longAmount').value is a string here
if (document.getElementById('longAmount').value > 0 && transStartOverhang + ((document.getElementById('longAmount').value - 1) * transSpacing) + transEndOverhang < transLength)
document.getElementById('longAmount').value = longAmount + 1; // longAmount is undefined.
// Again, makes no sense.
document.getElementById('longAmount').innerHTML = document.getElementById('longAmount').value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.