单个表单字段中的多个段落 - Jquery 在 keydown 上附加
我有一个非常基本的 CMS,它与 PHP 和 MySQL 组合在一起。一切都很好,除了我找不到一种方法使文本区域字段能够包含多个段落(例如博客文章、详细的服务描述)。数据存储在 MySQL 中,段落之间有
,但是当我调用它进行显示时,所有中断都被删除。 PHP 中没有任何内容可以提取 html(不修剪 html 实体、字符串替换等)。理想情况下,我能够将光标包裹在 就像我现在输入的这个文本区域一样。
今天我一直在尝试JQuery。
<script type="text/javascript">
$(document).ready(function(){
$('#thetest').keydown(function(event) {
if (event.keyCode == '13') {
$('#thetest').append('<p>Test</p>');
});
});
</script>
<form action="test_upload.php" enctype="multipart/form-data" method="POST">
<p>Content: </p>
<textarea wrap="virtual" id="thetest" name="thetest" value=""
autocomplete="off" rows="20"cols="100">
</textarea>
<input type="submit" name="submit" value="Upload" />
</form>
如果有人能指出我正确的方向,我将不胜感激。我认为应该有一个用于表单的 jquery 插件可以处理它,但我还没有找到。预先非常感谢。
I have a very basic CMS that I've put together with PHP and MySQL. Everything works great, except I can't find a way to make textarea fields capable of containing multiple paragraphs (e.g. blog posts, detailed service descriptions). The data is stored in MySQL with <br>
s between the paragraphs, but when I call it back for display, all breaks are removed. There is nothing in the PHP that would pull out html (no trimming of html entities, string replacing, etc.) Ideally, I'd be able to wrap the cursor in <p></p>
like this textarea I'm typing in now does.
Today I've been trying JQuery.
<script type="text/javascript">
$(document).ready(function(){
$('#thetest').keydown(function(event) {
if (event.keyCode == '13') {
$('#thetest').append('<p>Test</p>');
});
});
</script>
<form action="test_upload.php" enctype="multipart/form-data" method="POST">
<p>Content: </p>
<textarea wrap="virtual" id="thetest" name="thetest" value=""
autocomplete="off" rows="20"cols="100">
</textarea>
<input type="submit" name="submit" value="Upload" />
</form>
If anyone could point me in the right direction, I'd greatly appreciate it. I'd think there'd be a jquery plugin for forms that would handle it, but I haven't found one. Thanks very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的用户是输入段落标签,还是您想要 WYSIWYG 风格?如果是前者,你应该不会有问题(如果有的话,你可以在它进入之前对其进行 htmlentities() 并在获取它时进行解码)。如果是后者,我会在将 $_POST['thetest'] 字符串返回给您时对其进行处理,然后再将其放入数据库中。在存储之前,您可以使用
和
preg_replace() 换行符,当然可以使用更多换行符以使其可读。
或者你可以用
Are your users typing in the paragraph tags, or do you want it WYSIWYG style? If it's the former, you shouldn't have a problem (and if you do, you can just htmlentities() it before it goes in and decode when you fetch it). If it's the latter, I would process the $_POST['thetest'] string as it comes back to you before putting it in your database. You can preg_replace() your newline characters with
<p>
and</p>
before storing it, with more newline characters to make it readable of course.Or you can cheat with
<pre>
just to preserve white space, but that wouldn't be semantically responsible :p尝试使用 nl2br(htmlspecialchars(stripslashes($paragraphs))) ,它将准确地输出用户输入的文本。
Try using
nl2br(htmlspecialchars(stripslashes($paragraphs)))
, it will output the text exactly how the user inputs it.