如何显示< input type =''text'>没有在jQuery中创建一个文本框? [英] How to display <input type=''text'> without creating a textbox in jquery?

查看:106
本文介绍了如何显示< input type =''text'>没有在jQuery中创建一个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个评论框(textarea),用户在其中键入内容,当他点击输入时,该内容会自动显示在评论部分中。现在,当用户点击提交时,我正在执行以下代码,

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

通过上面的代码,用户键入的注释动态显示在 commentSection 。这很好,但当用户输入类似的东西时,

 < input type ='text'> 

然后在评论部分创建一个文本框。那么我有没有办法让这种事情发生?
在此先感谢。

解决方案

您必须将字符串转换为实体。定义这个函数:

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

然后在用户输入时运行以下代码:

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


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. 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.

解决方案

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:

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

这篇关于如何显示&lt; input type =''text'&gt;没有在jQuery中创建一个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆