javascript - 无法从文本框获取输入
问题描述
我正在尝试从文本框中获取输入,但它不起作用。
我也很难让我的变量在函数之间工作。
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>
解决方案
<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>
推荐阅读
- javascript - Javascript Validate Input fields + Select Field
- ios13 - 尝试在 iOS 13 或 tvOS 13 中使用 AVPictureInPictureController 时出错
- c# - 使用身份服务器 3 和身份服务器 4 创建的令牌保护 API
- php - 我想通过文本框值传递 Mysql 查询
- php - (Mahara) manually set cookies disappear on the next page
- javascript - 使用角度卡片时如何将页脚放置在页面底部的大小
- node.js - 如何将会话传递给 Mongoose 中的“Model.create()”。?
- angular - 如何删除angualr材质树中的节点
- android - 如何使用 VLC 作为实时流媒体服务器?
- javascript - 相机插件未在 Ionic 4 中打开相机