0

私が何を間違えたのかよくわかりません。しかし、nPrice と nAmount の 2 つのエラーが表示されません。ただし、私の名前と 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

2番目の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) {

&&さらに、(OR) 演算子ではなく (AND) 演算子を使用しているため、論理に誤りがあります||

于 2013-03-26T21:44:10.927 に答える