0
关于js验证输入框是否为空的疑问5
我在jsp页面中使用下面这个函数来验证对话框是否为空
function checkForm(){
if(document.form1.type.value=="")
{ form1.type.focus();
alert('该对话框不能为空);
return false;
}
}
<form name="form1" action="servlets/createservlet">
.
.
.
<input type="submit" name="submit" value="提交" onclick="checkForm()">
如果有对话框为空的话,js函数返回false,会弹出一个对话框提示你。
但是我点击“确定”后,为什么jsp页面上的数据还会被提交到servlet中去啊?
向各位大侠请教!!!!
第1个回答 2014-03-29
function checkForm(){
if(document.form1.type.value=="")
{ form1.type.focus();
alert('该对话框不能为空);
return false;
}
}
if(document.form1.type.value=="")
{ form1.type.focus();
alert('该对话框不能为空);
return false;
}else{
alert("if条件有问题");
}
} 这样子来检验下试试
if(document.form1.type.value=="")
{ form1.type.focus();
alert('该对话框不能为空);
return false;
}
}
document.form1.type???这里是不是有问题,如果这里报错,整个if语句都不执行,肯定不会return false
function checkForm(){if(document.form1.type.value=="")
{ form1.type.focus();
alert('该对话框不能为空);
return false;
}else{
alert("if条件有问题");
}
} 这样子来检验下试试
第2个回答 推荐于2017-09-17
function checkForm()
{
var _form = document.form1;
if(_form.type.value.replace(/^\s*|\s*$/g, "") == "")
{
_form.type.focus();
alert('该对话框不能为空 ! ');
return false;
}
else
{
return true;
}
}
<form name="form1" action="servlets/createservlet">
...
<input type="text" id="type" name="type" />
...
<input type="submit" name="submit" value="提交" onclick="return checkForm();">本回答被提问者和网友采纳
{
var _form = document.form1;
if(_form.type.value.replace(/^\s*|\s*$/g, "") == "")
{
_form.type.focus();
alert('该对话框不能为空 ! ');
return false;
}
else
{
return true;
}
}
<form name="form1" action="servlets/createservlet">
...
<input type="text" id="type" name="type" />
...
<input type="submit" name="submit" value="提交" onclick="return checkForm();">本回答被提问者和网友采纳