日期:2014-05-16  浏览次数:20332 次

为什么只检查标题的合法性,不检查内容?~~~

这是代码:
<script language="javascript">
function Check(){
if(topicCheck() && contentCheck())
return true;
else 
return false;
}
function topicCheck(){
var topic=document.getElementById("topic").value;
if(topic!="")
return true;
else
alert("主题不能为空!");
}
function contentCheck(){
var content=document.getElementById("content").value;
if(content!="")
return true;
else
alert("内容不能为空!");
}

</script>
<font color="white"><p>创建新帖</p>
<form action="creatTopicCl.php" method="POST" name="creatTopic" onsubmit="return Check();">
主题:<input type="text" name="topic" id="topic" size="40"  width="50" onchange="topicCheck();"><br><br>
内容:<textarea name="content" id="content" rows="10" cols="50" onchange="contentCheck();">
</textarea><br>
<input type="submit" name="submit" value="提交" class="sub">
<input type="reset" value="重填" class="sub">

------解决方案--------------------
改成这样

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
$("#submit").click(function(){
if($("#topic").val()==""){
alert("主题为空");
return false;
}else if($("textarea").val()==""){
alert("内容为空");
return false;
}else{
return true;
}
});
})
</script>
 <font color="white"><p>创建新帖</p>
 <form action="creatTopicCl.php" method="POST" name="creatTopic" />
主题:<input type="text" name="topic" id="topic" size="40"  width="50" /><br><br>
内容:<textarea name="content" id="content" rows="10" cols="50"/></textarea><br>
 <input type="submit" id="submit" value="提交" class="sub"/>
 <input type="reset" value="重填" class="sub"/> 


你那个原因出在var content=document.getElementById("content").value;
试着获取textarea标签
var content=document.getElementByTag("textarea").value;
不知道是不是这么写,反正大概是这个意思