1

我创建了一个如下的订单表单,其中包含多个复选框来添加项目,现在我想只在用户单击相关复选框时对禁用项目进行验证,请帮助我更正以下代码。

<script language="JavaScript">
function check_box2(status)
{
status=!status;
document.orderform.item2.disabled = status;
document.orderform.qty2.disabled = status;
}
function check_box3(status)
{
status=!status; 
document.orderform.item3.disabled = status;
document.orderform.qty3.disabled = status;
}
function check_box4(status)
{
status=!status; 
document.orderform.item4.disabled = status;
document.orderform.qty4.disabled = status;
}
</script>

<script language="JavaScript" src="gen_validatorv4.js" type="text/javascript" ></script>

<body onload="check_box2(status); check_box3(status); check_box4(status);">
<form action="order.php" method="POST" name="orderform" id="orderform" onSubmit="return validation();">
<table>
<tr><td valign="top"> <p>Order Item 1 :</p> </td><td> <select id="item1" name="item1">
      <option value="0" selected="selected"><p> [Select] </p></option>
      <option value="1"> <p> Product 1 </p></option>
      <option value="2"> <p> Product 2 </p> </option>
      <option value="3"> <p> Product 3 </p> </option>
</select> </td>

<td valign="top"><p>Quantity :</p></td><td><input style="width:30px;" name="qty1" type="text"></td> </tr>

<tr><td valign="middle"><input type="checkbox" name="check2" onclick="check_box2(this.checked)" >Add Item 2 : </td><td> <select id="item2" name="item2">
      <option value="0" selected="selected"><p> [Select] </p></option>
      <option value="1"> <p> Product 1 </p></option>
      <option value="2"> <p> Product 2 </p> </option>
      <option value="3"> <p> Product 3 </p> </option>
</select> </td>

<td valign="top"><p>Quantity :</p></td><td><input style="width:30px;" name="qty2" type="text"></td> </tr>

<tr><td valign="middle"> <input type="checkbox" name="check3" onclick="check_box3(this.checked)" >Add Item 3 :  </td><td> <select id="item3">
      <option value="0" selected="selected"><p> [Select] </p></option>
      <option value="1"> <p> Product 1 </p></option>
      <option value="2"> <p> Product 2 </p> </option>
      <option value="3"> <p> Product 3 </p> </option>
</select> </td>

<td valign="top"><p>Quantity :</p></td><td><input style="width:30px;" name="qty3" type="text"></td> </tr>

<tr><td valign="middle"> <input type="checkbox" name="check4" onclick="check_box4(this.checked)" >Add Item 4 :  </td><td> <select id="item4">
      <option value="0" selected="selected"><p> [Select] </p></option>
      <option value="1"> <p> Product 1 </p></option>
      <option value="2"> <p> Product 2 </p> </option>
</select> </td>

<td valign="top"><p>Quantity :</p></td><td><input style="width:30px;" name="qty4" type="text"></td> </tr>

</table>

<div><input name="submit" type="submit" value="Send"> </div>
</form>

<script  type="text/javascript">
    var frmvalidator  = new Validator("orderform");
    frmvalidator.addValidation("item1","dontselect=0", "Please select an item");
    frmvalidator.addValidation("qty1","req","Please enter your quantity");

    if  (document.orderform.item2.checked==true)
    {
       frmvalidator.addValidation("item2","dontselect=0", "Please select an item2");
       frmvalidator.addValidation("qty2","req","Please enter your quantity");
    }
</script>

</body>
4

2 回答 2

1

能够使用 gen_validatorv4.js 脚本中包含的 VWZ_IsChecked() 函数进行验证

frmvalidator.addValidation("item2","dontselect=0", "Please select an item2",
    "VWZ_IsChecked(document.forms['orderform'].elements['check2'],'check2')");


frmvalidator.addValidation("qty2","req","Please enter your quantity for item 2",
    "VWZ_IsChecked(document.forms['orderform'].elements['check2'],'check2')");
于 2012-09-15T14:01:37.617 回答
1

您只需添加一些条件:

if(document.getElementById('checkbox123').checked == true) {
  // validate code here
}

你也可以这样做:

if(document.orderform.checkbox123.checked == true) {
  // validate code here
}
于 2012-09-06T12:50:49.657 回答