简体   繁体   中英

Using Markdown when form is submitted by pressing enter

I have a Rails project, and I'd like to use Markdown in a form where the form is submitted with the enter key. I don't know if this is feasible because enter in Markdown is for new lines. Just wondering if I'm missing something. As far as I can tell, if I wanted to use Markdown, I'd have to use a submit button in my form.

If I really wanted the Markdown functionality in my form with the enter key as submit, I think I'd have to code each thing specifically. Something like:

$("textarea").keydown(function(e){
    // Enter was pressed with shift key
    if (e.keyCode == 13 && e.shiftKey)
    {
        // add <br> to html $("textarea").val();
        // prevent default behavior
        e.preventDefault();
    }
});

Just wanted to make sure I'm barking up the right tree. Thanks!

I think if you have a form with Markdown, you definitely need a button to submit form. If I see the multi-line text area (even without Markdown), I expect Enter to add a new line and not to submit the form.

Check how it works on other web-sites, for example this:

在此处输入图片说明

Update : OK, if you have to do this, here how I think it can be done. The idea is to submit the form if Enter is pressed without Shift (and prevent the default, so Enter doesn't go to the text). And Shift+Enter inserts the new line (looks like it just works, no need to do anything).

Example code:

 $("textarea").keypress(function(e){ //alert('key:' + e.keyCode); // Enter was pressed with shift key if (e.keyCode == 13 && !e.shiftKey) { e.preventDefault(); $('form').submit(); } }); $("form").submit(function(e) { alert('submitted'); e.preventDefault(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <form> <textarea></textarea> </form> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM