function validate(){
var subject = document.getElementById("subject").value;
var content = document.getElementById("content").value;
if (subject == "") {
alert("添加公告标题不能为空,请添加!");
document.getElementById("subject").focus();
return false;
}else if (content == "") {
alert("添加公告内容不能为空,请添加!");
document.getElementById("content").focus();
return false;
}else{
return true;
}
}
function save() {
if(validate()){
document.form1.submit();
}else{
return;
}
}当我只填了标题框(subject),没有填内容框(content)时,他就给我提交了,哪里出问题了?
var subject = document.getElementById("subject").value;
var content = document.getElementById("content").value;
if (subject == "") {
alert("添加公告标题不能为空,请添加!");
document.getElementById("subject").focus();
return false;
}else if (content == "") {
alert("添加公告内容不能为空,请添加!");
document.getElementById("content").focus();
return false;
}else{
return true;
}
}
function save() {
if(validate()){
document.form1.submit();
}else{
return;
}
}当我只填了标题框(subject),没有填内容框(content)时,他就给我提交了,哪里出问题了?
贴表单!用 input type=button
你上面的写法,按钮input的type应该是button吧。
你贴出来的代码没什么问题,最好把其他的也贴出来看看,要不我们只能猜是哪的原因
var content = document.getElementById("content").value;
把这两个值弹出来看下,也许这两句话报错了,后面就不执行了
或者try catch {return false;}试试是否依然提交了
应该是
if(){}
if(){}
function validate(){
var subject = document.getElementById("subject").value;
var content = document.getElementById("content").value;
if (subject == "") {
alert("添加公告标题不能为空,请添加!");
document.getElementById("subject").focus();
return false;
}//else去掉
if (content == "") {
alert("添加公告内容不能为空,请添加!");
document.getElementById("content").focus();
return false;
}//else去掉
return true;
}
function save() {
if(validate()){
document.form1.submit();
}else{
return;
} }
<input type="text" name="subject" id="subject" />内容
<fck:editor id="content" basePath="${pageContext.request.contextPath}/fckeditor/"
customConfigurationsPath="${pageContext.request.contextPath}/fckeditor/myconfig.js"
toolbarSet="Customer" width="100%" height="400"></fck:editor>
subject值是存在的,是不是因为内容用的是fck内容编辑器所以取不到值吗?
var subject = document.getElementById("subject");
var content = document.getElementById("content");
if (subject.length<=0) {
alert("添加公告标题不能为空,请添加!");
document.getElementById("subject").focus();
return false;
}if (content.length<=0) {
alert("添加公告内容不能为空,请添加!");
document.getElementById("content").focus();
return false;
}
}
第一:你在哪掉的save 把那段html发下
第二:你的FCK用document.getElementById可以的
第三:没有办法的办法 你可以打alert调试 看看代码哪出问题额
function save() {
if(validate()){
document.form1.submit();
}else{
return;
}
直接<input id="btn" type="button" onclick="return validate()">就可以了。何必复杂化?
这不是重点,重点是即使我再文本框(content)中添加了内容,我alert(content);出的东西依然是空的,也就是我用普通的方式拿不到文本编辑器(fck:editor)中的内容。
有没有好的实现验证文本编辑器非空的方法????