如何仅根据显示的项目处理错误检查

How do I handle error checking based only on Items displayed

本文关键字:处理 错误 检查 项目 何仅根 显示      更新时间:2023-09-26

再次问候助手,

我有这些剪:

<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>

这些都是相关的代码。

如果用户单击"是"单选按钮,-->中的所有表单字段都会显示出来。如果用户选中"否"单选按钮,则显示用于数据输入的唯一表单字段"价格"。

到目前为止还不错。我正在努力解决的问题是,如何确保我在上面发布的Javascript验证代码只对可见的表单字段发出警报。

我很抱歉这么说,但是,那个代码很讨厌!

更换这类线路:

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

带有:

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

然后,只有当:visible选择器显示时,您才能检查并验证它:

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

数组1将包含字段的ID,这些字段将显示为"是",数组2将是相同的,只是对于将显示为"否"的字段。

设置后,使用一个条件来检查选择了哪个单选按钮"是"或"否",因此您将检查匹配的字段大堆

如果选择"是",则:对于Array1中的每个元素,都有一个document.getElementById(elem)并对其进行验证。

需要一些代码,或者你有这个想法?