問候再次幫手,如何處理錯誤僅基於項目檢查顯示
我有這些剪:
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function validate() {
missinginfo = "";
if (document.form.catdescription.value == "0") {
missinginfo += "\n - Category CANNOT be blank";
}
if (document.form.pcode.value == "") {
missinginfo += "\n - Product Code CANNOT be blank";
}
if (document.form.pname.value == "") {
missinginfo += "\n - Product Name CANNOT be blank";
}
if (document.form.pdescription.value == "") {
missinginfo += "\n - Product Description CANNOT be blank";
}
if ((form.IsSpecial[0].checked == false) && (form.IsSpecial[1].checked == false)) {
missinginfo += "\n - please check Yes or No";
}
if ((document.form.pphoto.value == "") || (document.form.pphoto.value == "NA"))
{
missinginfo += "\n - Photo is either blank or does not contain NA";
}
if (document.form.unitprice.value == "") {
missinginfo += "\n - Price is NOT blank";
}
if (document.form.file2.value == "") {
missinginfo += "\n - Browse a picture to upload";
}
if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"Please ensure that:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
</script>
<script type="text/JavaScript">
<!-- Begin
function ChangeDiv(id)
{
if(id == "No")
{
document.getElementById('nof').style.display = "block";
document.getElementById('yesf').style.display = "none";
}
else
{
document.getElementById('nof').style.display = "none";
document.getElementById('yesf').style.display = "block";
}
}
// End -->
</script>
</head>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=4 BORDERCOLOR="YELLOW" >
<tr>
<td class="body1" div align="right">
<div id="yesf" style="display:none;">
Promo Start Date:
<input type="text" name="pstartdate" class="normaltxt"><br>
Promo End Date:
<input type="text" name="penddate" class="normaltxt"><br>
PROMO Price:
<input type="text" name="txtsprice" value="">
</div>
</td>
</tr>
<tr>
<td class="body1" div align="right">
<div id="nof">
Regular Price:
<input type="text" name="unitprice" value="">
</div>
</td>
</tr>
</table>
這些都是相關的代碼。
如果用戶點擊Yes單選按鈕,則會顯示 - >內的所有表單域。如果用戶選中「否」單選按鈕,則會顯示唯一的表單字段「price」,用於數據輸入。
到目前爲止這麼好。我正在努力解決的問題是如何確保我上面發佈的Javascript驗證代碼只會引發只有可見表單域的警報。
嘗試':visible'選擇器,並開始使用jQuery選擇器或至少'document.getElementById' – gdoron 2012-03-07 19:27:20