点击“Enter”键发送消息

时间:2017-06-15 15:49:37

标签: javascript jquery html

我设计了一个表单来发送带有提交按钮的消息,但对于每条消息,我都必须使用我的光标点击提交。如何通过点击键盘的“Enter”键来发送消息。这是我的代码

 <div class="message_post" id="myForm">
    <form action="" method="POST">

        <?php 
            if($user_to == "new") {

                echo "Send a message to your friend <br><br>"; ?>

                To: <input type='text' onkeyup='getUsers(this.value, "<?php echo $userLoggedIn; ?>")' name='q' placeholder='Search Username' autocomplete='off' id='search_text_input'>

                <?php
                echo "<div class='results'</div>";

            }
            else { // MESSAGE BOX

                if($userLoggedIn == $user_to)
                    echo "<p style='margin-left:275px;'>You can't send message to yourself</p>";
                else {

                echo "<textarea name='message_body' id='message_textarea' placeholder='Send a message'></textarea>";
                echo "<input type='submit' name='post_message' class='info' id='message_submit' value='Send'>";


                }


            }


         ?>


    </form>




 </div>

 <script>    // Javascript to automatically scrolling down to recent message

    var div = document.getElementById("scroll_messages");
    if(div != null) {
    div.scrollTop = div.scrollHeight;

    }
 </script>

1 个答案:

答案 0 :(得分:0)

这是用于侦听输入密钥并汇总表单的JavaScript代码

$('html').keydown(function(e){
  if(e.which==13){
    document.getElementById("myForm").submit();
  }
});