单击后提交textarea

时间:2015-05-01 17:57:27

标签: javascript php jquery forms

首先,这类问题有很多答案,但它们都不适合我,因为我使用多种形式。

我的问题是,我需要一个脚本,当单击输入时将在textarea中提交数据,但脚本必须使用下面的代码。

<?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的评论框),上面的脚本将无效。谢谢。

1 个答案:

答案 0 :(得分:4)

这个怎么样

  $(this).closest('form').submit();

而不是

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

这样,只提交当前textarea所属的表格。

相关问题