本文介绍了如何在javascript中将字母转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试创建一个简单的javascript程序,在该程序中,用户输入单词/句子,然后该程序显示您要输入的数字以在翻盖手机中输入文字。例如,如果我输入 add,它将输出 122。
I have been trying to create a simple javascript program where the user types in words/sentences, and the program shows the numbers you would enter to text it in a flip phone. For instance, if I entered "add" it would output "122".
我该怎么做?我尝试了很多不同的方法,但没有任何效果。似乎应该是一个简单的程序。您能帮我做到这一点,还是将我链接到类似程序?
How would I do this? I have tried a lot of different things, and nothing works. It seems like it should be a simple program. Can you either help me do this, or link me to a similar program?
谢谢。
推荐答案
function str2num(mystr) {
mystr = mystr.toUpperCase();
var conv = [];
var l = mystr.length;
for (var i=0; i conv[i] = mystr.charCodeAt(i)-65;
}
return conv;
}
用于电话转换器
如果链接断开,则代码如下:
in case the link breaks here's the code:
<script language="javascript" type="text/javascript">
<!-- Begin
function convert(input) {
var inputlength = input.length;
input = input.toLowerCase();
var phonenumber = "";
for (i = 0; i < inputlength; i++) {
var character = input.charAt(i);
switch(character) {
case '0': phonenumber+="0";break;
case '1': phonenumber+="1";break;
case '2': phonenumber+="2";break;
case '3': phonenumber+="3";break;
case '4': phonenumber+="4";break;
case '5': phonenumber+="5";break;
case '6': phonenumber+="6";break;
case '7': phonenumber+="7";break;
case '8': phonenumber+="8";break;
case '9': phonenumber+="9";break;
case '-': phonenumber+="-";break;
case 'a': case 'b': case 'c': phonenumber+="2";break;
case 'd': case 'e': case 'f': phonenumber+="3";break;
case 'g': case 'h': case 'i': phonenumber+="4";break;
case 'j': case 'k': case 'l': phonenumber+="5";break;
case 'm': case 'n': case 'o': phonenumber+="6";break;
case 'p': case 'q': case 'r': case 's': phonenumber+="7";break;
case 't': case 'u': case 'v': phonenumber+="8";break;
case 'w': case 'x': case 'y': case 'z': phonenumber+="9";break;
}
}
document.myform.number.value = phonenumber;
return true;
}
// End -->
</script>
<form name=myform>
<table border=0>
<tr>
<td>Alphanumeric #:</td>
<td><input type=text size=20 maxlength=20 name=alphabet value="Rachel"></td>
</tr>
<tr>
<td>Converted #:</td>
<td><input type="text" size=20 maxlength=20 name="number"></td>
</tr>
<tr>
<td align=center colspan=2><input type=button value="Convert" onClick="return convert(document.myform.alphabet.value)"></td>
</table>
</form>
这篇关于如何在javascript中将字母转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!