无法从文本框获取输入

时间:2019-03-15 18:51:58

标签: javascript html forms

我正在尝试从文本框中获取输入,但是它将无法正常工作。

我也很难让变量在函数之间工作。

var first_name = document.getElementById('first');
var last_name = document.getElementById('first');

function firstNameClick() {
  window.document.f.note.value = "Enter first name, then last name.";
  window.document.f.note.size = 25;
}

function firstNameLeave() {
  window.document.f.note.size = 10;
  window.document.f.note.value = "";
  first_name = document.getElementById('first');
  if ((first_name != "") && (last_name != "")) {
    window.document.f.firstDisplay.value = (first_name);
    window.document.f.firstDisplay.value = (last_name);
  } else {
    window.document.f.firstDisplay.value = "";
  }

}

function lastName() {
  last_name = document.getElementById('last');
  if ((first_name != "") && (last_name != "")) {
    window.document.f.lastDisplay.value = (last_name);
    window.document.f.firstDisplay.value = (first_name);
  } else {
    window.document.f.lastDisplay.value = "";
  }
}
<form name="f">
  <font>First Name</font>
  </br>
  <input type="text" id="first" name="first" size=35 onClick="firstNameClick();" onBlur="firstNameLeave();">
  </br>
  </br>
  <font>Last Name</font>
  </br>
  <input type="text" id="last" name="last" size=35 onBlur="lastName();">
  </br>
  </br>
  <font>Note</font>
  </br>
  <input type="text" name="note" size=10 value="" readonly>
  </br>
  </br>
  <font>Display</font>
  </br>
  <input type="text" name="firstDisplay" size=25 value="" readonly>
  </br>
  <input type="text" name="lastDisplay" size=25 value="" readonly>

</form>

1 个答案:

答案 0 :(得分:0)

<html>
<head></head>
<body>
  <form name="f">
    <font>First Name</font>
    <br>
    <input type="text" id="first" name="first" size=35 onClick="firstNameClick();" onBlur="firstNameLeave();">
    <br>
    <br>
    <font>Last Name</font>
    <br>
    <input type="text" id="last" name="last" size=35 onBlur="lastName();">
    <br>
    <br>
    <font>Note</font>
    <br>
    <input type="text" name="note" size=10 value="" readonly>
    <br>
    <br>
    <font>Display</font>
    <br>
    <input type="text" name="firstDisplay" size=25 value="" readonly>
    <br>
    <input type="text" name="lastDisplay" size=25 value="" readonly>

  </form>
  
  <script>
    var first_name = document.getElementById('first').value; //add .value
    var last_name = document.getElementById('last').value; // add .value and use 'last' in attribute selector you had first
    window.document.f = document.querySelector('f');

    function firstNameClick() {
      window.document.f.note.value = "Enter first name, then last name.";
      window.document.f.note.size = 25;
    }

    function firstNameLeave() {
      window.document.f.note.size = 10;
      window.document.f.note.value = "";
      first_name = document.getElementById('first').value; // add.value
      if ((first_name != "") && (last_name != "")) {
        window.document.f.firstDisplay.value = (first_name);
        window.document.f.lastDisplay.value = (last_name); //use lastDisplay
      } else {
        window.document.f.firstDisplay.value = "";
      }
    }

    function lastName() {
      last_name = document.getElementById('last').value; //add .value
      if ((first_name != "") && (last_name != "")) {
        window.document.f.lastDisplay.value = (last_name);
        window.document.f.firstDisplay.value = (first_name);
      } else {
        window.document.f.lastDisplay.value = "";
      }
    }
  </script>
</body>

</html>