首先,此类问题有很多答案,但没有一个适合我,因为我使用多种表单。
我的问题是,我需要一个脚本,该脚本将在单击输入时提交文本区域内的数据,但该脚本必须与下面的代码一起使用。
<?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();
这样,只会提交当前textarea所属的表单。
关于javascript - 单击 Enter 时提交文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29992310/