[英]How do I make text appear in text field with javascript?
I'm relatively new to JavaScript and I can't seem to figure out how to output text in a textfield in HTML. 我对JavaScript相对陌生,似乎无法弄清楚如何在HTML文本字段中输出文本。 I've looked at many solutions and although I'm very close (I think I am), I still can't get it to function properly. 我已经看过许多解决方案,尽管我非常接近(我想是),但是我仍然无法使其正常运行。
I'm trying to sort a series of numbers by separating them with "; " and wish to show the result in the bottom text field by ascending or descending order. 我正在尝试通过用“;”分隔它们来对一系列数字进行排序,并希望通过升序或降序在底部文本字段中显示结果。 The action is done by pressing the button to sort them. 通过按按钮对它们进行排序来完成操作。
When trying to debug it, it seems that the variables are being assigned their values as they should, but the variable "output" just won't show up in the "Sorted Numbers" textfield. 尝试调试时,似乎已为变量分配了应有的值,但变量“输出”将不会显示在“排序编号”文本字段中。 My code is incomplete as I'm just trying to test this for an ascending order before I add in the descending order. 我的代码不完整,因为在添加降序之前,我只是尝试对它进行升序测试。
Here is my code... 这是我的代码...
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>Sorting Numbers</title>
</head>
<body>
Enter Numbers Here: <input type = "text" id = "numInput"><br><br>
Select Option: <select id = "menu">
<option value = "ascending">Ascending</option>
<option value = "descending">Descending</option>
</select><br><br>
Sorted Numbers: <input type = "text" id = "numOut"><br><br>
<button onclick = "sortNums()">Sort Numbers</button>
<script type = "text/javascript">
function sortNums() {
var choice = document.getElementById("menu");
var numbers = document.getElementById("numInput").value;
var output = document.getElementById("numOut").value;
if(choice.value = "ascending") {
arr = numbers.split('; ').sort().join('; ');
output = arr;
}
}
</script>
</body>
</html>
Because you never assigned output
to it afterwards: 因为您之后从未分配output
:
if(choice.value = "ascending") {
arr = numbers.split('; ').sort().join('; ');
output = arr;
document.getElementById("numOut").value = output;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.