<!DOCTYPE html>
<html>
<head>
    <title>Convert Text</title>
    <script>
        function convertText() {
            var inputText = document.getElementById("inputText").value;
            var convertedText = "";
            var outputNumber = "";
<pre><code>        var mapping = {
            &quot;承诺&quot;: &quot;1&quot;,
            &quot;处理&quot;: &quot;2&quot;,
            &quot;转告&quot;: &quot;3&quot;,
            &quot;无人接听&quot;: &quot;4&quot;,
            &quot;停机&quot;: &quot;5&quot;,
            &quot;关机&quot;: &quot;6&quot;,
            &quot;会&quot;: &quot;7&quot;,
            &quot;占线&quot;: &quot;8&quot;,
            &quot;拒接&quot;: &quot;9&quot;,
            &quot;后&quot;: &quot;0&quot;
        };

        // 检查输入文本是否为数字序列
        if (!isNaN(inputText)) {
            // 将数字转换成文字
            for (var i = 0; i &lt; inputText.length; i++) {
                var number = inputText.charAt(i);
                for (var key in mapping) {
                    if (mapping.hasOwnProperty(key) &amp;&amp; mapping[key] === number) {
                        convertedText += key;
                        break;
                    }
                }
            }

            // 显示转换结果
            document.getElementById(&quot;outputNumber&quot;).value = inputText;
            document.getElementById(&quot;convertedText&quot;).value = convertedText;
        } else {
            // 将文字转换成数字
            outputNumber = &quot;&quot;;

            // 移除空格
            inputText = inputText.replace(/\s+/g, &quot;&quot;);

            // 将转换后的文字序列拆分为单个文字
            var textArray = inputText.split(&quot;&quot;);

            // 遍历文字序列,转换成数字
            for (var i = 0; i &lt; textArray.length; i++) {
                var text = textArray[i];
                if (mapping.hasOwnProperty(text)) {
                    outputNumber += mapping[text];
                }
            }

            // 显示转换结果
            document.getElementById(&quot;outputNumber&quot;).value = outputNumber;
            document.getElementById(&quot;convertedText&quot;).value = inputText;
        }
    }
&lt;/script&gt;
</code></pre>
</head>
<body>
    <h1>Convert Text</h1>
    <label for="inputText">输入文本:</label>
    <input type="text" id="inputText"><br><br>
    <button onclick="convertText()">转换</button><br><br>
    <label for="outputNumber">输出数字:</label>
    <input type="text" id="outputNumber" readonly><br><br>
    <label for="convertedText">转换后的文本:</label>
    <input type="text" id="convertedText" readonly>
</body>
</html
帮我修改代码现在文字转数字不正确!DOCTYPE html html head titleConvert Texttitle script function convertText var inputText = documentgetElementByIdinputTextvalue; var convertedT

原文地址: http://www.cveoy.top/t/topic/ismf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录