|
带jQuery的
- //将输入框id和按钮id,改成对应的id即可
- $("#输入框id").keydown(function(e){
- if(e.keyCode == 13){
- $("#按钮id").click();
- }
- });
复制代码
不带jQuery的
- <script>
- window.onload=function(){
- //event : 'keydown',value : this.value, keyCode : event.keyCode
- var x = document.getElementById("aa");
- if (x.addEventListener) {
- x.addEventListener("keydown", function(event) {
- if(event.keyCode == 13){
- document.getElementById("bb").onclick();
- }
- });
- } else if (x.attachEvent) {// IE 8 及更早 IE 版本
- x.attachEvent("onkeydown", function(event) {
- if(event.keyCode == 13){
- document.getElementById("bb").onclick();
- }
- });
- }
- }
- </script>
- <input id="aa" type=text />
- <input id="bb" type=button value ="OK" />
复制代码
|
|