0

不太确定我做错了什么。但是我的两个错误没有显示,nPrice 和 nAmount。但是,我的名字和 nSmokes 显示。我认为这与我正在使用的 if else 的多重条件有关。

我真的很想听听你的想法。

提前致谢。

function doValidate()
{

//alert("Working");

document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value);
document.myForm.nAmount.value=parseInt(document.myForm.nAmount.value);
document.myForm.nSmokes.value=parseInt(document.myForm.nSmokes.value);

//alert(document.myForm.nPrice.value);

//alert(document.myForm.nAmount.value);

//alert(document.myForm.nSmokes.value);



errorCount = 0;
errorMsg = ""

if( document.myForm.name.value == "") {

        errorMsg = errorMsg + "You need to enter a value!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 

}

if (document.myForm.nPrice.value < 0 && document.myForm.nPrice.value > 20.00) {

        errorMsg = errorMsg + "You need to enter a value between 0 and 20!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}

if (document.myForm.nAmount.value < 0 && document.myForm.nAmount.value > 40) {

        errorMsg = errorMsg + "You need to enter a value between 0 and 40!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}
if (document.myForm.nSmokes.value < 0) {

        errorMsg = errorMsg + "<br/>Either your doing a great job or you put in that you     smoked 0 today!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}

if (errorCount > 0) {
        return false;
} else {
        return true;
}
}

HTML

<form name="myForm" method="post" id="myForm" onsubmit="return doValidate();">

  <table width="600" border="1" cellspacing="4" cellpadding="4">
   <tr>
     <td colspan="2" align="center"><img src="quitSmoking.jpg"  width="278"/></td>
   </tr>
   <tr>
     <td colspan="2" align="center">Quit Smoking Calculator by Your Name</td>
  </tr>
   <tr>
    <td>Your Name</td>
    <td><input type="text" name="name" id="name"  />&nbsp;</td>
   </tr> 
   <tr>
     <td>Date you Quit</td>
     <td><input name="startDate" type="text"> 
   <input type="button" value="select" onclick="displayDatePicker('startDate');">&nbsp;</td>
    </tr>
     <tr>
       <td>Price of a pack</td>
      <td><input type="text" name="nPrice" id="nPrice" value="8.00" />&nbsp;</td>
     </tr>
       <tr>
       <td>How many in a pack</td>
       <td><input type="text" name="nAmount" id="nAmount" value="20" />&nbsp;</td>
     </tr>
     <tr>
       <td>Number of cigarettes you smoked a day</td>
       <td><input type="text" name="nSmokes" id="nSmokes" value="25" />&nbsp;</td>
     </tr>
     <tr>
      <td colspan="2" align="center"><input type="submit" name="btnSubmit" id="btnSubmit" value="Calculate Savings" /></td>
    </tr>
     <tr>
       <td colspan="2" align="center"><div id="errors"></div></td>
     </tr>
     <tr>
       <td id="rowResult" colspan="2" align="center">
       <input type="hidden" name="nResult" id="nResult" value="" />
       &nbsp;</td>
     </tr>
   </table>

    </form>
4

2 回答 2

3
document.myForm.nPrice.value < 0 && document.myForm.nPrice.value > 20.00

一个值怎么可能小于 0大于20?您正在那里寻找 OR。

document.myForm.nPrice.value < 0 || document.myForm.nPrice.value > 20.00

介于 0 到 20 之间

document.myForm.nPrice.value > 0 && document.myForm.nPrice.value < 20.00

您在第二个 if 表达式中也犯了类似的错误。

于 2013-03-26T21:42:39.753 回答
0

这条线不会像你想象的那样工作:

document.myForm.nSmokes.value = parseInt(document.myForm.nSmokes.value);

一旦您再次将值放回表单中,该值将立即转换回字符串,这基本上就是您对上述行所做的事情。相反,您需要将解析后的值存储在变量中,然后在验证中使用这些变量。

var nSmokes = parseInt(document.myForm.nSmokes.value);

然后像这样使用它:

if (nSmokes < 0) {

此外,您的逻辑有错误,因为您使用的是&&(AND) 运算符,而不是||(OR) 运算符,这更有意义。

于 2013-03-26T21:44:10.927 回答