简体   繁体   English

如何使用javascript使文本显示在文本字段中?

[英]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.

相关问题 如何使 html 文本区域出现 javascript? - How do i make an html text area appear javascript? 提交后,如何使输入字段中的文本显示在屏幕上? - How do I make the text from an input field appear on the screen after it is submitted? 如何使单击的文本出现在表单字段上? - how to make clicked text appear on form field? 如何修正我的文字使其显示在一行上? - How do I fix my text to make it appear on one line? 如何使文本再次出现和消失 - How do I make the text appear and disappear again 如何制作将文本​​放置在文本字段中的下拉菜单 - How Do I Make a Dropdown Menu That Places A Text In a text Field 当我点击圆圈时,如何使用 Javascript 使圆圈改变颜色? 另外,如果所有圆圈都是红色,我该如何显示文字? 一世 - How do I make the circles change color with Javascript when I click on them? Also, how do I make text appear if all circles are red? I Javascript:当选中/选中分配给它的单选按钮时,如何使输入/文本字段为必需字段? - Javascript: How do I make an input/text field mandatory(required) when the radio button assigned to it is checked/selected? 如何使文本框中的文本看起来像 textarea - How do I make the text inside a text box to appear like textarea 如何使用 javascript 显示表单? - How do I make a form appear with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM