如何正确使用html pls中文本框的值帮助我

时间:2019-03-28 08:54:23

标签: javascript if-statement

我想使用文本框中的值,但失败了,所以我尝试使用常量值,现在出现了NAN错误。我在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
}

1 个答案:

答案 0 :(得分:0)

您要混合使用innerHTMLvalue以获得相同的ID。如果该ID是文本框,则应使用.value。另外,要转换字符串(来自文本区域的文本),可以使用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;