jquery - 使用 AJAX 在同一页面中显示文本区域字段

标签 jquery ajax forms magento

我尝试在 showContent div 中使用 keyup 键入时显示文本区域输入的值(稍后我将添加更多代码)。 但我不擅长 Ajax 和/或 JQuery,需要一些帮助。

formPage.phtml 中的表单(我不知道是否因为它是一个重要的 phtml 文件):

<form id="answer_form" class="form" method="post">
   <textarea class="input-text" id="content" name="content" id="answer_content" title="Content"></textarea>
   <div id="showContent"><span></span></div>
</form>

我想在输入时在 showContent div 中显示它的内容,换句话说,在每个字母之后显示。

非常感谢!

最佳答案

我建议使用 input 事件而不是 keyup 因为当您跟踪用户输入时它会更有效:

$('body').on('input', '#content', function(){
    $('#showContent').text( $(this).val() );
})

希望这有帮助。

$('body').on('input', '#content', function(){
    $('#showContent').text($(this).val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="answer_form" class="form" method="post">
  <textarea class="input-text" id="content" name="content" id="answer_content" title="Content"></textarea>
  <div id="showContent"><span></span></div>
</form>

关于jquery - 使用 AJAX 在同一页面中显示文本区域字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023553/

相关文章:

javascript - 页面未满时将页脚推到底部

delphi - 如何在其所有者表单之上显示模态表单(其所有者是否设置为 fsStayOnTop),就像 TOpenDialog 一样

javascript - 在页面刷新时更改获取 URL 参数

javascript - 在下拉列表中单击复选框时,复选框保持选中状态

jQuery 移动 : Breaks When Reloading/Deep Linking/Bookmarking Pages Added to DOM via AJAX

javascript - 不使用 HTML 表单通过 ajax POST 上传图片

html - 更改表单上的 flex-direction 属性?

Chrome 中的 jQuery Dropkick : Dropdowns not auto-closing

javascript - 如何通过单击页面中的其他位置取消下拉列表 - stopPropagation?

javascript - 如何使用它所在的 div 调整固定的 div/nav 大小?