当前位置:   article > 正文

javascript实现数字随机软键盘_js 随机数字键盘

js 随机数字键盘

新建一个softkeyboard.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="softkeyboard.aspx.cs" Inherits="softkeyboard" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>随机数字软键盘</title>
  6. <script>
  7. window.onload = function () {
  8. password1 = null;
  9. initCalc();
  10. }
  11. var password1;
  12. var checkSoftKey;
  13. function addValue(newValue) {
  14. var str = Calc.password.value;
  15. if (str.length < password1.maxLength) {
  16. Calc.password.value += newValue;
  17. }
  18. if (str.length <= password1.maxLength) {
  19. password1.value = Calc.password.value;
  20. }
  21. }
  22. function setpassvalue() {
  23. var longnum = Calc.password.value.length;
  24. var num
  25. num = Calc.password.value.substr(0, longnum - 1);
  26. Calc.password.value = num;
  27. var str = Calc.password.value;
  28. password1.value = Calc.password.value;
  29. }
  30. function OverInput() {
  31. var str = Calc.password.value;
  32. password1.value = Calc.password.value;
  33. closekeyboard();
  34. Calc.password.value = "";
  35. password1.readOnly = 1;
  36. }
  37. function closekeyboard(theForm) {
  38. softkeyboard.style.display = "none";
  39. }
  40. function showkeyboard() {
  41. randomNumberButton();
  42. var th = password1;
  43. var ttop = th.offsetTop;
  44. var thei = th.clientHeight;
  45. var tleft = th.offsetLeft;
  46. while (th = th.offsetParent) { ttop += th.offsetTop; tleft += th.offsetLeft; }
  47. softkeyboard.style.top = ttop + "px";
  48. softkeyboard.style.left = Math.max(tleft + 160, 0) + "px";
  49. softkeyboard.style.display = "block";
  50. password1.readOnly = 1;
  51. password1.blur();
  52. document.all.useKey.focus();
  53. }
  54. function initCalc() {
  55. for (var i = 0; i < Calc.elements.length; i++) {
  56. if (Calc.elements[i].type == "button") {
  57. var str1 = Calc.elements[i].value;
  58. str1 = str1.trim();
  59. var thisButtonValue = Calc.elements[i].value;
  60. thisButtonValue = thisButtonValue.trim();
  61. if (thisButtonValue.length == 1) {
  62. Calc.elements[i].ondblclick = Calc.elements[i].onclick = function () {
  63. var thisButtonValue = this.value;
  64. thisButtonValue = thisButtonValue.trim();
  65. addValue(thisButtonValue);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. String.prototype.trim = function () {
  72. return this.replace(/(^\s*)|(\s*$)/g, "");
  73. }
  74. function randomNumberButton() {/*随机数字*/
  75. var randomNum;
  76. for (var i = 0; i < 21; i++) {
  77. randomNum = parseInt(Math.random() * 10);
  78. var tmp = Calc.button_number0.value;
  79. Calc.button_number0.value = eval("Calc.button_number" + randomNum + ".value");
  80. eval("Calc.button_number" + randomNum + ".value=tmp");
  81. }
  82. }
  83. </script>
  84. </head>
  85. <body>
  86. <div id="softkeyboard" name="softkeyboard" style="position: absolute; left: 142px;
  87. top: 23px; z-index: 99; display: none; background-color: #99cc99; width: 178px;text-align: center;">
  88. <form id="Calc" name="Calc" action="" method="post" autocomplete="off">
  89. <input type="hidden" value="" name="password">
  90. <input type="hidden" value="ok" name="action2">
  91. <br />
  92. <!--<span style="font-weight: bold; font-size: 13px; color: #075BC3">密码输入器</span>-->
  93. <input id="useKey" type="button" value="使用键盘输入" οnclick="password1.readOnly=0;password1.focus();closekeyboard();password1.value='';" style="width:172px;">
  94. <input type="button" name="button_number1" value=" 1 ">
  95. <input type="button" name="button_number2" value=" 2 ">
  96. <input type="button" name="button_number3" value=" 3 "><br />
  97. <input type="button" name="button_number4" value=" 4 ">
  98. <input type="button" name="button_number5" value=" 5 ">
  99. <input type="button" name="button_number6" value=" 6 "><br />
  100. <input type="button" name="button_number7" value=" 7 ">
  101. <input type="button" name="button_number8" value=" 8 ">
  102. <input type="button" name="button_number9" value=" 9 "><br />
  103. <input type="button" name="button_number0" value=" 0 ">
  104. <input name="button10" type="button" value="删 除" οnclick="setpassvalue();" οndblclick="setpassvalue();" style="width: 52px">
  105. <input name="button12" type="button" οnclick="OverInput();" value="确 定" style="width: 52px"><br />
  106. </form>
  107. </div>
  108. <input maxlength="6" size="20" type="password" name="password" οndblclick="password1=this;showkeyboard();"
  109. οnclick="password1=this;showkeyboard();" title="请填写密码" />
  110. <input type="button" value="显示" οnclick="alert(password1.value);" />
  111. </body>
  112. </html>


效果如图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/106799
推荐阅读
相关标签
  

闽ICP备14008679号