如果表单中没有数据或者是多个空格,则判断为空,如何实现?
第1个回答 推荐于2016-03-16
<script type="text/javascript">
function test(){
var value = document.getElementById("username").value;
value = value.replace(/\s/img, "");
if(value == ""){
alert("请输入数据");
}
}
</script>
<input type="text" name="username" id="username">
<input type="button" value="检测" onclick="test()">本回答被提问者采纳
function test(){
var value = document.getElementById("username").value;
value = value.replace(/\s/img, "");
if(value == ""){
alert("请输入数据");
}
}
</script>
<input type="text" name="username" id="username">
<input type="button" value="检测" onclick="test()">本回答被提问者采纳
第2个回答 2008-10-17
在<form 中加上onSubmit="return check();">
在JSP页中加以下代码
<script type="text/javascript">
function check(){
var chks = document.all.tags("INPUT");
for(var i=0;i<chks.length;i++)
{
if(chks[i].type.toLowerCase()=="text")
{
if(chks[i].value.length==0 ){
alert("添加文本不能为空!!!");
return false;
}
}
}
return true;
}
</script>
在JSP页中加以下代码
<script type="text/javascript">
function check(){
var chks = document.all.tags("INPUT");
for(var i=0;i<chks.length;i++)
{
if(chks[i].type.toLowerCase()=="text")
{
if(chks[i].value.length==0 ){
alert("添加文本不能为空!!!");
return false;
}
}
}
return true;
}
</script>
第3个回答 2008-10-17
用.trim()只能去左右两边的空格,不能去中间的空格,可以用正则表达式做!
第4个回答 2008-10-17
if(document.getElementById("id1").value.trim()=""){ //这样就可以
执行动作
}
执行动作
}
第5个回答 2015-10-23
var str="";
if(str==""){
}
就这样就可以了
if(str==""){
}
就这样就可以了