2017-06-15 187 views
0

我設計了一個表單來發送帶有提交按鈕的消息,但是對於每條消息,我必須使用我的光標單擊submit.How纔會通過點擊我的「Enter」鍵發送消息鍵盤。這裏是我的代碼通過點擊'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> 

+5

[提交上的Enter鍵與jQuery形式η]可能的複製(https://stackoverflow.com/questions/699065/submitting-a-form-on-enter-with-jquery) – Qirel

+0

PHP與客戶端交互無關。 – chris85

+0

這是所有相關的代碼?如果你在一個'form'中有一個'',那麼點擊表單中的Enter將會提交它。 https://codepen.io/anon/pen/LLbxyd –

回答

0

這是JavaScript代碼來聽回車鍵,並請跟隨形式

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