按Enter键提交包含contenteditable div的附加表格

时间:2014-08-03 19:31:05

标签: jquery html

我想提交一个表单,其中包含按Enter键的contenteditable div。使用jquery .before()将表单附加到div中。

<form class="send" id="send_it<?php echo $seed['idd']; ?>" method="post" action="#">
    <div id="input_msg" contenteditable="true" 
          class="input<?php echo $seed['idd']; ?>" 
          style="width:210px; position:fixed; bottom:0;">
    </div>
    <input type="submit" value="send"  style="display:none;" />
</form>

1 个答案:

答案 0 :(得分:0)

这会做你想要的:

$(document).keyup(function(e){
    if(e.keyCode== 13 || e.which== 13) { //if enter key is pressed
        var contentEditableValue=$('#input_msg').text(); //get the div value
        $('.send').append('<input type="hidden" value="'+contentEditableValue+'" id="valueToSend">'); //add a dummy input to the form to send the value
        $('.send').submit(); //submit the form
    }
});