textarea保留空格和回车符的格式提交表单

目的:为了保持提交表单前输入框内的内容格式和用户在看到自己的数据之后保持一致



test.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>textarea测试</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<style type="text/css" media="screen">
		.cc{width:600px;height:100px;}
		#d1{background:#88eeff;font-size:14px;}
		#zwtj{font-size:14px;}
	</style>
	
	<script type="text/javascript">
	
	// 扩展String类型的替换全部函数
	String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) 
	{ 
		if (!RegExp.prototype.isPrototypeOf(reallyDo)) 
		{
			return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); 
		}
		else 
		{
			return this.replace(reallyDo, replaceWith); 
		}
	}

	// 获取转义后的字符串 保留回车符、保留空格符
	function getTextAreaValue(strId)
	{
		var eTxt = document.getElementById(strId);
		var str = eTxt.value;
		str = str.replaceAll(" ", " ",true);
		str = str.replaceAll("\n", "<br/>",true);
		return str;
	}
	
	// 在div当中看转换后的textarea中的文本
	function showText()
	{
		var eD2 = document.getElementById("d1");
		
		eD2.innerHTML = getTextAreaValue("zwtj");
	}

	</script>
	</head>

	<body>

		<textarea name="zwtj" cols="80" rows="5" id="zwtj" ></textarea>
		<br/>
		<input type="button" value="查看文本↓" οnclick="showText()" />
		<br/>
		<div class="cc" id="d1">
		@sonikk 2013-4-11 11:03:30
		</div>
			
	</body>
</html>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值