点击进入提交文本区域 [英] Submitting textarea when clicked enter

查看:60
本文介绍了点击进入提交文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,针对此类问题有很多答案,但由于我使用的是多种形式,因此它们对我都不起作用.

First of all, there are many answers for this type of questions but none of them work for me because I am using multiple forms.

我的问题是,我需要一个脚本,该脚本将在单击Enter时在textarea内提交数据,但是该脚本必须与以下代码一起使用.

My question is, I need a script which will submit the data inside the textarea when clicked enter but the script must work with the code below.

<?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>

我以前在下面使用此表单,如果一页中有多个表单,则该表单不起作用.

I used to use this one below, which doesn't work if there are multiple forms in one page.

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

为了明确起见,如果我在页面中创建单个表单(例如订阅网站新闻),则上面的脚本将起作用.但是,如果页面中有多个表单(例如,每条帖子后都有Facebook的评论框),则上述脚本将无法使用.谢谢.

Just to make it clear, If I create a single form in a page (like subscription for newsletter of a site) the script above will work. But if you have more than 1 form in a page (like facebook's comment boxes right after every post) the script above will not work. Thank you.

推荐答案

如何解决

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

代替此

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

通过这种方式,仅提交当前文本区域所属的表单.

In this way, only the form to which the current textarea belongs will be submitted.

这篇关于点击进入提交文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆