2012-03-07 16 views
0

問候再次幫手,如何處理錯誤僅基於項目檢查顯示

我有這些剪:

<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驗證代碼只會引發只有可見表單域的警報。

+0

嘗試':visible'選擇器,並開始使用jQuery選擇器或至少'document.getElementById' – gdoron 2012-03-07 19:27:20

回答

0

我很抱歉地說,但是,該代碼是討厭的!

取代那些種系:

if (document.form.catdescription.value == "0") {... 

有了:

if ($('#catdescription').val() == "0") {... 

然後你就可以檢查驗證它只有當它的可見伊斯利與:visible選擇:

if ($('#catdescription:visible').val() == "0"){ 
    missinginfo += "\n  - Category CANNOT be blank"; 
} 
+0

嗨Gdoron,是代碼jquery?我使用常規的JavaScript。如果jQuery,你有完整的代碼? – Kenny 2012-03-07 19:45:39

+0

@Kenny。是的,這是'jQuery' ...我不能給你一個「完整的代碼」,因爲它不可能確切地知道你想要做什麼以及什麼時候做什麼。只需使用這些代碼片段,並學習[jQuery](http://api.jquery.com/) – gdoron 2012-03-07 20:46:36

+0

好的,您能否請傳遞一些好的鏈接來指導我?我還沒有找到任何有意義的東西。我清楚地說明了我想要完成的事情。 – Kenny 2012-03-07 20:57:37

0

數組1將包含將顯示爲「是」的字段的ID, 數組2將相同,僅用於顯示爲「否」的字段。

設置後,使用一個條件來檢查選中哪個單選按鈕 「是」或「否」,並且由於此條件,您將檢查匹配的 數組的字段。

如果選擇「是」,那麼:對於Array1中的每個元素,有document.getElementById(elem)並驗證它。

需要一些代碼或你有想法?

+0

非常感謝Ofir。代碼示例會很棒。 – Kenny 2012-03-07 19:44:34