How to make “Enter” key in a textarea submit a form

前端 未结 6 899
太阳男子
太阳男子 2020-12-16 08:49

I have a chat that uses textarea instead of text for obvious reasons. This is why each time members hit ENTER they get a new line instead of sending the message. I would li

相关标签:
6条回答
  • 2020-12-16 09:26

    Try this (note that when you press ENTER go submit, but SHIFT+ENTER go new line.

    $("#textareaId").keypress(function (e) {
        if(e.which == 13 && !e.shiftKey) {        
            $(this).closest("form").submit();
            e.preventDefault();
        }
    });
    
    0 讨论(0)
  • 2020-12-16 09:28

    Use <input type=text> instead of <textarea>. Then you have much better chances. In textarea, hitting Enter is supposed to mean a line break in text, not submission of text.

    0 讨论(0)
  • 2020-12-16 09:33
    function KP()
    {    
    var x = event.keyCode;
       if(x==13)
            {
                // alert (x);
                document.message.submit();
            }
    }
    

    php

    echo '<textarea  name="usrMsg" id="usrMsg" OnKeyPress="KP();">'txt'</textarea>';
    
    0 讨论(0)
  • 2020-12-16 09:34

    Vanilla JavaScript solution

    function submitOnEnter(event){
        if(event.which === 13){
            event.target.form.dispatchEvent(new Event("submit", {cancelable: true}));
            event.preventDefault(); // Prevents the addition of a new line in the text field (not needed in a lot of cases)
        }
    }
    
    document.getElementById("usermsg").addEventListener("keypress", submitOnEnter);
    

    If you would prefer to not submit if Shift is clicked all you need to do is change line 2 to:

    if(event.which === 13 && !event.shiftKey){
    

    To clear the textarea just add this inside the if statement body

    event.target.value = "";
    

    To use this with Internet Explorer 11 change line 3 to:

    var newEvent = document.createEvent("Event");
    newEvent.initEvent("submit", false, true);
    event.target.form.dispatchEvent(newEvent);
    

    But wait? Why not use event.target.form.submit();?

    When you invoke the submit method of the form the event listeners will not be called.


    Demo:

    function submitOnEnter(event){
        if(event.which === 13){
            event.target.form.dispatchEvent(new Event("submit", {cancelable: true}));
            event.preventDefault(); // Prevents the addition of a new line in the text field (not needed in a lot of cases)
        }
    }
    
    document.getElementById("usermsg").addEventListener("keypress", submitOnEnter);
    
    document.getElementById("form").addEventListener("submit", (event) => {
        event.preventDefault();
        console.log("form submitted");
    });
    <form id="form">
        <textarea id="usermsg"></textarea>
        <button type="Submit">Submit</button>
    </form>

    0 讨论(0)
  • 2020-12-16 09:35

    I have created a jsfiddle with an example on how to do it with jQuery and the keypressfunction and which property: http://jsfiddle.net/GcdUE/

    Not sure exactly what you are asking for more than this, so please specify your question further if possible.

    $(function() {
        $("#usermsg").keypress(function (e) {
            if(e.which == 13) {
                //submit form via ajax, this is not JS but server side scripting so not showing here
                $("#chatbox").append($(this).val() + "<br/>");
                $(this).val("");
                e.preventDefault();
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-16 09:46

    Well, supposing you were using jquery it would be a simple listener on your input field:

    In your footer before </body>:

    <script type="text/javascript">
    $(document).ready(function(){
        $('#usermsg').keypress(function(e){
          if(e.which == 13){
               // submit via ajax or
               $('form').submit();
           }
        });
    });
    </script>
    

    In your html head add this:

    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script> 
    

    but if js or jquery is out of the question then perhaps update your question to specifically exclude it.

    0 讨论(0)
提交回复
热议问题