我有一种情况,按Enter键,文本区域的内容应该被提交。但我希望内容保留在textarea中。这样以前输入的文本就可见了。 我尝试了下面的代码,但它不会工作
<script type="text/javascript">
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
myfield.submit();
return true;
}
function populatedText(){
var myvalue = document.getElementById("cmd");
document.getElementById("cmd").innerHTML = myvalue.value;
}
}
</script>
</head>
<body>
<FORM onKeyPress="submitenter(this,event)" class="cmdForm" method="post">
<textarea rows="15" cols="100" name="cmdEntered" id="cmdEntered">$cmdEntered</textarea>
</FORM>
答案 0 :(得分:0)
if you are using php you can use the below code or if you are using some other server side language change it to the corresponding one. Basic idea is same just submit the form to the same page .
</head>
<body>
<?php
$cmdEntered = $_POST['cmdEntered'];
?>
<FORM class="cmdForm" method="post" action="same_page_name">
<textarea rows="15" cols="100" name="cmdEntered" id="cmdEntered">
echo $cmdEntered;
</textarea>
</FORM>
答案 1 :(得分:-1)
你可以使用缓存来存储输入的类型!这样您就可以在提交表单后在textarea中显示相同的文本。
如果您仍然希望在提交文本后仍然保持在同一页面上,最好使用ajax提交您点击输入按钮时触发的数据。
答案 2 :(得分:-1)
您的代码应该是:
<script type="text/javascript">
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
myfield.submit();
populatedText();
return true;
}
}
function populatedText(){
var textarea_value = document.getElementById("cmdEntered").value;
document.getElementById("cmdEntered").value = textarea_value;
}
</script>
</head>
<body>
<FORM onKeyPress="submitenter(this,event)" class="cmdForm" method="post">
<textarea rows="15" cols="100" name="cmdEntered" id="cmdEntered">$cmdEntered</textarea>
</FORM>