已经判断了输入的内容不为空,但是当输入都是空格的时候,也判断内容是有值的。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/jquery-1.10.1.min.js"></script>
</head>
<style type="text/css">
.demo {
width: 200px;
height: 100px;
}
.textarea {
width: 100%;
height: 100%;
}
.error {
line-height: 30px;
}
.btn {
margin-top: 10px;
}
</style>
<body>
<div class="demo">
<textarea class="textarea" name="" rows="" cols=""></textarea>
</div>
<div class="error"></div>
<button class="btn">点击</button>
</body>
<script type="text/javascript">
//输入内容不能能为空
// $(".btn").click(function() {
// if($(".textarea").val() == "") {
// $(".error").html("内容不能能为空")
// }
// })
//但是当输入的内容只为空格时,也是可以提交的,所以要去除空格。
$(".btn").click(function() {
if($(".textarea").val().trim() == "") {
$(".error").html("内容不能能为空")
}
})
</script>
</html>