我想在键和点击时触发一个功能

时间:2016-11-14 00:57:07

标签: javascript jquery onclick onkeypress

单击“计算”按钮时,我当前运行了一个函数。

<button class="hvr-shrink"  type="submit" value="submit" id="submit"> Calculate </button>

我希望当我不仅单击提交按钮而且还单击 ENTER KEY

时,该函数会运行

功能与完美一起工作只是希望它在用户点击进入时也能正常工作

Check the javascript here!

HTML to the function

<html>

<head>

    <title>Project</title>

    <link href="../CSS files/style.css" rel="stylesheet" type="text/css">
    <link href="../CSS files/hover.css" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800' rel='stylesheet' type='text/css'>

</head>

<body>
    <div id="home">
        <a href="home.html"> 
            <img class="hvr-shadow" src="../../misc/hoem.png" id="homeicon">
        </a>
    </div>
    <h1>Geometric Sequence</h1>
    <h2>Find any term of a Geometric Sequence</h2>

        <input id="firstTerm" placeholder="First Term?">
        <input id="nTerm" placeholder="Nth Term?">
        <input id="commonR" placeholder="Common Ratio?">

        <button class="hvr-shrink" type="submit" value="submit" id="submit"> Calculate </button>



        <p id="term"> </p>

    </div>

    <script type="text/javascript" src="../math fxn (js)/geoseq.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.0/firebase.js"></script>


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</body>



</body>

2 个答案:

答案 0 :(得分:0)

将此代码添加到您的页面:

<script>
$(function(){
    $('input#commonR').keypress(function(event) {
        var keycode = event.keyCode || event.which;
        if(keycode == '13') {
            $('#submit').click();    
        }
    });
}); //END document.ready
</script>

答案 1 :(得分:0)

查看我在配偶上工作的小提琴,让我知道这是否是你需要的,并让它更新到你想要的。 。 。 https://jsfiddle.net/e2nf73bu/1/

$(document).on('keypress',function(event){
   if ( event.which == 13 ) {
         $('#submit').click()
  }
})