简体   繁体   English

显示方式 <input type=''text'> 没有在jQuery中创建一个文本框?

[英]How to display <input type=''text'> without creating a textbox in jquery?

I have a comment box(textarea) in which the user types something and when he hits enter that thing is automatically displayed in 'comment section'. 我有一个注释框(文本区域),用户在其中输入内容,当他点击时,该内容将自动显示在“注释部分”中。 Now when the user hits submit I'm executing the following code, 现在,当用户点击“提交”时,我正在执行以下代码,

var comment = $("#commentBox").val();
var commentSection = $("#commentSection");
comment.appendTo(commentSection);

By the above code the comment typed by user is dynamically displayed in the commentSection . 通过以上代码,用户键入的注释将动态显示在commentSection This works fine but when user types something like, 这可以正常工作,但是当用户键入类似内容时,

<input type='text'>

in the comment box then a textbox is created within the comment section. 在注释框中,然后在注释部分中创建一个文本框。 So is there a way through which I could not let this happen? 那么有没有办法让我不让这种事情发生呢? Thanks in advance. 提前致谢。

One way would be to just append the data as .text 一种方法是将数据附加为.text

Something like this: 像这样:

var comment = $("#commentBox").val();
var commentSection = $("#commentSection");
commentSection.text(comment);

Edit: To append to an existing part of the comment, replace: 编辑:要追加到注释的现有部分,请替换:

commentSection.text(comment);

with: 与:

commentSection.text(commentSection.text() + comment);

You have to convert the string to entities. 您必须将字符串转换为实体。 Define this function: 定义此功能:

function htmlencode(str) {
    return str.replace(/[&<>"']/g, function($0) {
        return "&" + {"&":"amp", "<":"lt", ">":"gt", '"':"quot", "'":"#39"}[$0] + ";";
    });
}

Then run the following code when the user hits enter: 然后在用户按下Enter键时运行以下代码:

var comment = htmlencode($("#commentBox").val());
var commentSection = $("#commentSection");
comment.appendTo(commentSection);

尝试这个 ,

div.insertAdjacentHTML( 'beforeend', comment);

You can use 您可以使用

var commentText = $("#commentBox").text();

but this do not clean html tags on your string, additionally you can use a function to do this 但这不会清除字符串上的html标签,此外,您可以使用函数来执行此操作

function RemoveHTMLTags(vals) {
            var regX = /(<([^>]+)>)/ig;
            var html = vals;
            return (html.replace(regX, ""));
        }

and then you use: 然后您使用:

var finalComment = RemoveHTMLTags(commentText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM