如何将文本框的值转换为数字
- 作者: admin
- 时间:
- 分类: JavaScript
我想问问.我如何开始对此进行编码?
我想将TextBox的值转换为数字.
例如,TextBox的值为"box"
而且每个字母都有正确的值.
B=1
O=3
X=2
...诸若此类.
嗯,我可以用Java脚本或php制作吗?
# 回答1
如果您希望在用户将内容键入文本"框"时进行转换,则必须使用JavaScript.如果希望在用户向服务器提交表单时进行转换,则必须使用php
下面是一个使用onChange()事件处理程序更改内容的js脚本.您可以使用regExp使其更快.或者,您甚至可以在用户使用onkeypress()事件处理程序键入每个字符时对其进行转换.
如果您希望在用户将内容键入文本"框"时进行转换,则必须使用JavaScript.如果希望在用户向服务器提交表单时进行转换,则必须使用php
下面是一个使用onChange()事件处理程序更改内容的js脚本.您可以使用regExp使其更快.或者,您甚至可以在用户使用onkeypress()事件处理程序键入每个字符时对其进行转换.
选择 | 换行 | 行号
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script type="text/javascript">
- function convertTxtBox(obj){
- //capture the id and value of the control element being processed
- var elm = obj.id;
- var val = obj.value;
- //create an object with conversion values
- lookup = {b:1, o:3, x:2};
- //initilize return value
- var retVal = '';
- //loop through each letter in text box and convert
- for (var i = 0, l = val.length; i < l; i++) {
- //get the current charcter in the string
- var re = val[i];
- //replace numeric value found in lookup object or return same character if not found
- retVal += ( lookup[re] ) ?lookup[re] : re;
- }
- //replace the text box value with converted value
- document.getElementById(elm).value = retVal;
- }
- </script>
- </head>
- <body>
- <input type="text" id="txtBox" onchange="convertTxtBox(this);"/>
- </body>
- </html>
# 回答2
感谢你的帮助..
感谢你的帮助..
标签: Javascript