1. 程式人生 > >回車鍵 執行js程式碼 {鍵盤事件}

回車鍵 執行js程式碼 {鍵盤事件}

    onkeypress=function(event){
        if(event.keyCode == 13){
            $('#btn').click();
        }
    }; 

完整 程式碼

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
>
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>成品繫結半成品列表</title> <script src="./js/jquery-2.1.1.min.js"></script> <script src="./js/jquery.paginationnew.js"></script> </head> <style> body{ background: #DDEEF2
; }
section h3,.form-div{ border:1px solid #BBDDE5; background: #F4FAFB; padding:5px; } section table{ margin:0 auto; width:100%; border-top:1px solid #EEF8F9; border-left:1px solid #EEF8F9; } section table th{ background
: #BBDDE5
; border-right:1px solid #EEF8F9; border-bottom:1px solid #EEF8F9; line-height:35px; }
section table tr{ background: #fff; } section table td{ border-right:1px solid #EEF8F9; border-bottom:1px solid #EEF8F9; line-height:27px; } tr:hover{ background-color:#F4FAFB; } .form-div{ margin-bottom:15px; } .form-div input[name='keyword']{ width:200px; } .active{ padding:5px 10px; border-radius:10px; background-color:#80BDCB; color:#fff; } .jump-ipt{ width: 60px; } .box a{ padding:5px; color:#192E32; } #cangku_log{ height: 22px; }
</style> <body> <section> <h3>成品繫結半成品列表</h3> <div class="form-div"> <input type="text" value="" name="keyword" size="15" id="keyword" placeholder="請輸入產品名稱"> <input type="submit" value=" 搜尋 " class="button" id="btn"> </div> <table cellspacing="0" cellpadding="0" border="0" id="tab"> </table> <input type="hidden" id="count" value="{$count}"> <div class="box" style="float: right;margin-top:15px;"></div> </section> <script> $(function(){ $('.box').pagination({ jump:true, coping:true, totalData:$("#count").val(), showData:13, keepShowPN:true, homePage:'首頁', endPage:'末頁', prevContent:'上頁', nextContent:'下頁', callback:function(api){ var index = api.getCurrent(); InitTable(index); } }); InitTable(1); function InitTable(index){ $.ajax({ type: "POST", url: 'goodsbcp_cp.php', data: {pageIndex: index, act: 'ajaxall', pageSize: 13, keyword: $('#keyword').val()}, //提交兩個引數:pageIndex(頁面索引),pageSize(顯示條數) success: function (data) { var data = JSON.parse(data); var str = '<tr><th>編號</th><th>產品名稱</th><th>規格</th><th width="110px;">操作</th></tr>'; $.each(data, function (index, obj) { str += '<tr>'; str += '<td>' + obj.goods_id + '</td>'; str += '<td>' + obj.goods_name + '</td>'; str += '<td>' + obj.goods_arrt_name + '</td>'; if(obj.tag == 'tag'){ str += '<td align="center"><a href="goodsbcp_cp.php?act=bcp_bind&goods_id='+obj.goods_id+'&attr_id='+obj.lib_id+'"><button>已繫結</button><button style="margin-left: 2px;">解綁</button></a></td>'; }else{ str += '<td align="center"><a href="goodsbcp_cp.php?act=bcp_bind&goods_id='+obj.goods_id+'&attr_id='+obj.lib_id+'"><button>去繫結</button></a></td>'; } str += '</tr>'; }) $('#tab').html(str); } }); } $('#btn').click(function(){ $.ajax({ type:'post', url:'goodsbcp_cp.php', async: false, data:{act:'countall',keyword:$('#keyword').val()}, success:function(count){ $('#count').val(count); $('.box').pagination({ jump:true, coping:true, keepShowPN:true, totalData:$("#count").val(), showData:13, homePage:'首頁', endPage:'末頁', prevContent:'上頁', nextContent:'下頁', callback:function(api){ var index = api.getCurrent(); InitTable(index); } }); } }) InitTable(1); }) }); onkeypress=function(event){ if(event.keyCode == 13){ $('#btn').click(); } }; </script> </body> </html>

onkeypress 事件
http://www.w3school.com.cn/jsref/event_onkeypress.asp
Keycode對照表
https://jingyan.baidu.com/article/fec7a1e5d6700a1190b4e725.html