textarea存起来的数据把空格也存起来

textarea的属性wrap="hard"可以把换行的内容也存起来。
<html>

	<head>
		<title>这是一个小测试</title>
		<meta name="" content="" charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	</head>

	<body>
		<div class="">
			<textarea name="" rows="10" cols="100" wrap="hard"></textarea>
		</div>
		<div class="">
			<button class="btn1">点击提交</button>
			<button class="btn2">点击获取</button>
		</div>
		<div class="content">

		</div>
	</body>
	<script src="jq.js"></script>
	<script type="text/javascript">
		var getFormatCode = function(strValue) {
			return strValue.replace(/
/g, '<br/>').replace(/
/g, '<br/>').replace(/s*/g, '');
		}//这个js一定不能丢
		$('.btn1').on('click', function() {
			$.ajax({
				type: "get",
				url: "http://*/login?text=" + getFormatCode($('textarea').val()),
				async: true,
				success: function(res) {

					if(res.code == 0) {
						alert('提交成功')
					}
				}
			});
		})

		$('.btn2').on('click', function() {
			$.ajax({
				type: "get",
				url: "http://*/text",
				async: true,
				success: function(res) {
					if(res.code == 0) {
						$('.content').html(res.msg)
					} else {
						alert('fail')
					}
				}
			});
		})
	</script>

</html>

  

这样就把多行文本的回车也存了起来

原文地址:https://www.cnblogs.com/yiyi17/p/8710300.html