modalDialog上的onkeypress事件

时间:2016-02-21 00:14:04

标签: javascript css html5 modal-dialog onkeypress

我有这段代码:

<a href="#openModal">Open Modal</a>
<div id="openModal" class="modalDialog">
<div>
    <a href="#close" title="Close" class="close">X</a>
    <h2>Modal Box</h2>
    <input type="radio" value="rdRec" checked="checked" />Rec
    <input type="radio" value="rdPot" />Pot
    <input type="button" id="btnOk" value="ok" onkeypress="checkey();">
</div>

<script type="text/javascript">
  function checkey()
  {
     var code = window.event.keyCode;
     if(code == 13) 
     {      
     document.getElementById('btnOk').click();
     return false;
     }
  }
</script>

我想要的只是当弹出窗口打开并选择/选中单选按钮Rec时我想点击输入键并自动触发btnOk ......

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的答案就是之前回答的问题:Use Javascript to change which submit is activated on enter key press

document.onkeypress = processKey;

function processKey(e)
{
    if (null == e)
        e = window.event ;
    if (e.keyCode == 13)  {
        document.getElementById("btnOk").click();
    }
}