【发布时间】:2012-05-30 02:37:55
【问题描述】:
这个脚本的功能主要是我想要的:当一个单选复选框没有被选中时发出警报。但是,如果选择了所有按钮,我需要它提交的表单......这就是我挂断电话的地方。现在,如果选择了所有字段和按钮,那么我仍然会收到带有 var alertMsg 的警报。有什么想法吗?
function submitform() {
var sizeChoice = ""
var size = document.store.on1.length
var fontChoice = ""
var len = document.store.on2.length
var materialChoice = ""
var material = document.store.on3.length
var treatmentChoice = ""
var treatment = document.store.on4.length
var a = document.forms["store"]["item_name"].value;
var alertMsg = "Please Choose a:"
for(i = 0; i < size; i++) {
if(document.store.on1[i].checked) {
sizeChoice = document.store.on1[i].value
}
}
for(i = 0; i < len; i++) {
if(document.store.on2[i].checked) {
fontChoice = document.store.on2[i].value
}
}
for(i = 0; i < material; i++) {
if(document.store.on3[i].checked) {
materialChoice = document.store.on3[i].value
}
}
for(i = 0; i < treatment; i++) {
if(document.store.on4[i].checked) {
treatmentChoice = document.store.on4[i].value
}
}
if(a == null || a == "") alertMsg += "\n" + "Name" + "\n";
if(sizeChoice == "") {
alertMsg += "Size" + "\n"
}
if(fontChoice == "") {
alertMsg += "Font" + "\n"
}
if(materialChoice == "") {
alertMsg += "Material" + "\n"
}
if(treatmentChoice == "") {
alertMsg += "Treatment" + "\n"
} {
alert(alertMsg)
};
return false;
document.forms["form"].submit();
};
【问题讨论】:
-
你在最后一个
if语句中有一个语法错误,看起来像是一个额外的括号。另外,您知道i是全局的,您可能想在某个地方定义它。
标签: javascript html validation