shiftKey事件属性不起作用

时间:2014-04-20 11:56:54

标签: javascript html

我有两个提交按钮:html部分中的'positive'和'negative'。我想要发生的是当用户按下SHIFT KEY然后在身体的任何地方进行moused时,应该提交POSITIVE BUTTON,并且按下CTRL KEY并执行mousedown时应该提交NEGATIVE BUTTON。我的代码如下:

<head>
    <script>
        function check(event)
        {
        if (event.shiftKey==1)
        {
            var b1=document.getElementById('btn1');
            b1.submit();
        }

        if (event.ctrlKey==1)
        {
            var b1=document.getElementById('btn1');
            b2.submit();
        }

        }
 </script>
</head>
<body onmousedown="check(event)">
    <form method="post" action="fetch_page.php">
   <input type="submit" name="submit1" value="Positive" id="btn1" class="btn btn-primary"></input>
   <input type="submit" name="submit2" value="Negative" id="btn2" class="btn btn-primary"></input>
    </form>
</body>

有人能告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:1)

<head></head>
<body>
    <form method="post" action="fetch_page.php">
        <input type="submit" name="submit1" value="Positive" id="btn1" class="btn btn-primary"></input>
        <input type="submit" name="submit2" value="Negative" id="btn2" class="btn btn-primary"></input>
    </form>
    <script>

        document.addEventListener('click', function(e) {
            if (e.shiftKey) document.getElementById('btn1').click();
            if (e.ctrlKey)  document.getElementById('btn2').click();
        }, false);
    </script>
</body>