今天做项目的时候遇到点问题 有一个大文本框,输入很多汉字 当然还要空格 分断
但是当我提交到数据库之后,查询出的结果很不令人满意,居然密密麻麻的一大片
无奈了,这时,白总给了我一个很好用的js代码,很好用,这里共享一下
function HTMLEncode(text){
//text = text.replace(/&/g, "&") ;
text = text.replace(/"/g, """) ;
//text = text.replace(/</g, "<") ;
//text = text.replace(/>/g, ">") ;
text = text.replace(/'/g, "’") ;
text = text.replace(/\ /g," ");
text = text.replace(/\n/g,"<br/>");
//text = text.replace(/\t/g," ");
return text;
}
用法如下:
第一步
form表单
<form action="xx.jsp" method="post" id="formone">
<textarea name="ttcont" id="ttcont" cols="50" rows="20">
</textarea>
<input type="button" οnclick="subform" value="提交"/>
</form>
第二步
<script>
//转码的方法
function HTMLEncode(text){
//text = text.replace(/&/g, "&") ;
text = text.replace(/"/g, """) ;
//text = text.replace(/</g, "<") ;
//text = text.replace(/>/g, ">") ;
text = text.replace(/'/g, "’") ;
text = text.replace(/\ /g," ");
text = text.replace(/\n/g,"<br/>");
//text = text.replace(/\t/g," ");
return text;
}
//按钮调用的方法
function subform(){
//获取文本框值
var tempvar=document.getElementByIdx_x("ttcont").value;
//进行转码操作,然后重新赋值
document.getElementByIdx_x("ttcont").value=HTMLEncode(tempvar);
//提交
document.getElementByIdx_x("formone").submit();
}
</script>
但是当我提交到数据库之后,查询出的结果很不令人满意,居然密密麻麻的一大片
无奈了,这时,白总给了我一个很好用的js代码,很好用,这里共享一下
function HTMLEncode(text){
}
用法如下:
第一步
form表单
<form action="xx.jsp" method="post" id="formone">
</form>
第二步
<script>
//转码的方法
function HTMLEncode(text){
}
//按钮调用的方法
function subform(){
//获取文本框值
var tempvar=document.getElementByIdx_x("ttcont").value;
//进行转码操作,然后重新赋值
document.getElementByIdx_x("ttcont").value=HTMLEncode(tempvar);
//提交
document.getElementByIdx_x("formone").submit();
}
</script>