单击Enter时提交Textarea



首先,此类问题有很多答案,但是它们对我来说都不适用,因为我使用了多种形式。

我的问题是,我需要一个脚本,当单击Enter时,将在文本方面提交数据,但脚本必须与下面的代码一起使用。

<?php
    $inputValue = NULL;
    $id = "[1,2,3];"
    if(isset($_POST['inputName'])){
        $inputValue = $_POST['inputName'];
        echo "<br>Input Value: " . $inputValue;
    }
    if(isset($_GET['id'])){
        $getValue = $_GET['id'];
        echo "<br>Get Value: " . $getValue;
    }
?>
<html>
    <head>
        <title>Multiple Forms in One Page</title>
    </head>
    <body>
        <br>
        <a href="index.php">Main Page</a>
        <br>
        <?php for($i = 0; $i < 3; $i++){ ?>
        <form action="index.php?id=<?php echo $id[$i] ?>" method="post" name="formName">
            <textarea name="inputName"></textarea>
            <input type="submit" name="submitName">
        </form>
        <?php } ?>
    </body>
</html>

我曾经在下面使用它,如果一页中有多个表格,则该

无效。

<script>
    $(function() {
        $('textarea#inputName').on('keydown', function(e) {
            if(e.keyCode == 13 && !e.shiftKey){
                document.getElementById('formName').submit();
            }
        });
    });
</script>

只是为了清楚地表明,如果我在页面中创建一个单个表单(例如网站的新闻通讯订阅),上面的脚本将起作用。但是,如果您的页面中有超过1个表格(例如,在每篇文章之后的Facebook的评论框)中,上面的脚本将无法使用。谢谢。

这个

怎么样
  $(this).closest('form').submit();

而不是这个

  document.getElementById('formName').submit();

以这种方式,只有当前文本属性所属的形式才能提交。

最新更新