1

i have a website with 3 pages. each page has a form with two input fields. i am trying to make a popup number-keypad that will populate what ever input field called it. below is that base code i keep coming back to.

<html>
<head><title>test</title></head>
<body>

<script> function num(id) { return document.getElementById(id); } </script>

<form action="/unitPage" method="POST" style=" text-align:center;">
Prefix: <input id="num" name"prefix" type="text" onfocus="num('keypad').style.display='inline-block';"/>

Number: <input id="num" name"number" type="text" pattern="[0-9]{6}" onfocus="num('keypad').style.display='inline-block';"/>
</form>

<div id="keypad" style="display:none; background:#AAA; vertical-align:top;">
<input type="button" value="7" onclick="num('num').value+=7;"/>
<input type="button" value="8" onclick="num('num').value+=8;"/>
<input type="button" value="9" onclick="num('num').value+=9;"/><br/>
<input type="button" value="4" onclick="num('num').value+=4;"/>
<input type="button" value="5" onclick="num('num').value+=5;"/>
<input type="button" value="6" onclick="num('num').value+=6;"/><br/>
<input type="button" value="1" onclick="num('num').value+=1;"/>
<input type="button" value="2" onclick="num('num').value+=2;"/>
<input type="button" value="3" onclick="num('num').value+=3;"/><br/>
<input type="button" value="X" onclick="num('keypad').style.display='none'"/>
<input type="button" value="0" onclick="num('num').value+=0;"/>
<input type="button" value="&larr;" onclick="num('num').value=num('num').value.substr(0,num('num').value.length-1);"/>
</div>

</body>
</html>

is there a way of making one number key pad that i call from any page or do i need to make the above for each input?

thanks

4

1 回答 1

1

一种可能的解决方案是您将 javacript 调用添加到您的标题中(通过您当前调用 document.getElementById(id) 调用的位置)来操作该 HTML 的 DOM,并将您的 div 插入其中。我会在每个页面上为该元素推荐一个 shell div,然后 javascript 只需通过 id(“键盘”)抓取该 div 并插入内部 HTML。

编辑:只要您在 3 个页面之间共享 javascript 文件,您只需执行一次 javascript。

于 2013-11-12T21:51:32.863 回答