【问题标题】:I need help to convert numbers to words [duplicate]我需要帮助将数字转换为单词[重复]
【发布时间】:2019-06-23 09:37:34
【问题描述】:

我需要将数字更改为 1=A,2=B 之类的单词。当我输入类似12 的输入时,结果应该是AB,如果只在P 标签内输入2 结果必须是B

<input type="text" id="nums">
<button id="btn"></button>
<p id="words"></p>

<script type="text/javascript">

    script here...

</script>

【问题讨论】:

  • 欢迎来到 Stack Overflow!请使用tour(您将获得徽章!)并通读help center,尤其是How do I ask a good question? 您最好的选择是进行研究,search 以获取有关 SO 的相关主题,然后试一试. 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您遇到的问题。人们会很乐意提供帮助。

标签: javascript string input onclick numbers


【解决方案1】:
<input type="text" id="nums">
        <button id="btn" onclick="run()">run</button>
        <p id="words"></p>
        <script>
        function run(){

            var alfa='abcdefghijklmnopqrstuvwxyz';
            var alfa_array = alfa.split(""); //this creates an array which contain whole characters 
            var num=document.getElementById("nums").value;
            var number_array = num.split(""); //this creates an array which contain whole numbers in input

//then you can go through each number inside number_array and use that number as key in alfa_array.this gives you matching character for each number.then join all characters together.

            var result='';
            for(var i in number_array){            
            result+=alfa_array[parseInt(number_array[i])-1];
            }

        document.getElementById("words").html=result;
        }
        </script>

这样就可以了。

【讨论】:

  • 请解释拒绝投票的原因。jsfiddle.net/kmwdh9bj 它工作正常
  • 它可能被否决了,因为您只发布了代码而没有任何解释。
  • Äsiri,现在改善这篇文章还为时不晚。考虑在答案本身中添加一些段落文本来解释您解决此问题的方法。这可能对未来的读者有用。
  • 好的.. 感谢您通知我。添加 cmets 以解释每个步骤。
  • 感谢您将此答案标记为正确答案
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2018-12-13
相关资源
最近更新 更多