[英]Javascript passing data between text boxes
I'm working on getting data to go from one textbox to another using javascript. 我正在使用javascript将数据从一个文本框转到另一个文本框。 Im new to Javascript and im getting a document undefined or null error. 我是Java的新手,正在获取文档未定义或空错误。
<!DOCTYPE html>
<html>
<head>
<script>
function doit() {
window.document.form1.txtbox1.value= window.document.form2.txtbox2.value;
}
</script>
</head>
<body>
<form name="form1">
Enter your name:
<input type="text" name="txtbox1" value="">
<input type="button" name="btn1" value="Click" onclick="doit()">
</form>
<br><br><br>
<form name="form2">
Results:
<input type="text" name="txtbox2" value="">
</form>
</body>
</html>
It seems that you are trying access the element as a property of the DOM. 似乎您尝试将元素作为DOM的属性进行访问。
Instead, you should use document.getElementsByName
method. 相反,您应该使用document.getElementsByName
方法。
Revised function: 修改功能:
function doit(){
// The [0] is for accessing the first item.
// If you are unfamiliar with arrays, visit
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
document.getElementsByName("txtbox1")[0].value = document.getElementsByName("txtbox2")[0].value;
}
You need to switch (swap) them : window.document.form1.txtbox1.value= window.document.form2.txtbox2.value;
您需要切换(交换)它们: window.document.form1.txtbox1.value= window.document.form2.txtbox2.value;
When you click a button - you set the value of the second input to the first one and probably the second input called 'Result' is empty. 单击按钮时-将第二个输入的值设置为第一个输入,可能称为“结果”的第二个输入为空。
Try: 尝试:
function doit() {
window.document.form2.txtbox2.value = window.document.form1.txtbox1.value;
}
There should not be any error, it's just pass data in another direction as you probably expect. 不应有任何错误,它只是按照您可能期望的方向传递数据。
Give a unique ID to both the input element. 给两个输入元素一个唯一的ID。
<input type="text" id="txtbox1" value="">
<input type="text" id="txtbox2" value="">
and in function doit() 和功能doit()
document.getElementById("txtbox2").value = document.getElementById("txtbox1").value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.