帮我修改代码现在文字转数字不正确!DOCTYPE html html head titleConvert Texttitle script function convertText var inputText = documentgetElementByIdinputTextvalue; var convertedT
<!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 = {
"承诺": "1",
"处理": "2",
"转告": "3",
"无人接听": "4",
"停机": "5",
"关机": "6",
"会": "7",
"占线": "8",
"拒接": "9",
"后": "0"
};
// 检查输入文本是否为数字序列
if (!isNaN(inputText)) {
// 将数字转换成文字
for (var i = 0; i < inputText.length; i++) {
var number = inputText.charAt(i);
for (var key in mapping) {
if (mapping.hasOwnProperty(key) && mapping[key] === number) {
convertedText += key;
break;
}
}
}
// 显示转换结果
document.getElementById("outputNumber").value = inputText;
document.getElementById("convertedText").value = convertedText;
} else {
// 将文字转换成数字
outputNumber = "";
// 移除空格
inputText = inputText.replace(/\s+/g, "");
// 将转换后的文字序列拆分为单个文字
var textArray = inputText.split("");
// 遍历文字序列,转换成数字
for (var i = 0; i < textArray.length; i++) {
var text = textArray[i];
if (mapping.hasOwnProperty(text)) {
outputNumber += mapping[text];
}
}
// 显示转换结果
document.getElementById("outputNumber").value = outputNumber;
document.getElementById("convertedText").value = inputText;
}
}
</script>
</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
原文地址: http://www.cveoy.top/t/topic/ismf 著作权归作者所有。请勿转载和采集!