js 转换文本框的值(转)
2013-10-30 11:52
501 查看
今天做项目的时候遇到点问题 有一个大文本框,输入很多汉字 当然还要空格 分断
但是当我提交到数据库之后,查询出的结果很不令人满意,居然密密麻麻的一大片
无奈了,这时,白总给了我一个很好用的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"
onclick="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){
//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"
onclick="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>
相关文章推荐
- jsp 页面 Cookie 操作及判断
- jsp 页面 播放视频 前提是flv格式…
- jsp 分页
- js添加Flash方法(转)
- JavaScript验证URL地址的正则(转…
- JSON就是一个对象的数组
- JS操作JSON总结
- xe5 JSON
- 用javascript模拟select达到美化效果
- jscript调用bat注意事项
- adp.net 和js 判断一个值是否在数组中
- JS将html转换成txt
- javascript函数的表示形式
- Knockout.js是什么?
- json需要的jar
- om.ibm.ws.jsp.translator.JspTranslationException:JSPG0227E解决办法
- om.ibm.ws.jsp.translator.JspTranslationException:JSPG0227E解决办法
- JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化
- 前台界面超全的js问题大全
- 跨域利器JSONP(转)