2013-04-23 42 views
1

我有一個Javascript表單正在運行一個HTML表單,該表單根據用戶輸入的值執行一些計算,然後將結果輸入到其他字段。這一切都很好。這裏的HTML是什麼樣子:Javascript - 檢查表單域的值,並顯示錯誤消息,如果值爲0

<table width="100%" border="0" cellspacing="0" cellpadding="10" class="border" id="calcs"> 
    <tr class="d_green"> 
    <td colspan="4">LAST YEAR</td> 
    </tr> 
    <tr class="l_white"> 
    <td width="53%">number of instances in the last year </td> 
    <td width="13%" align="right"><input name="textfield1" type="text" class="input_b_r" id="textfield1" value="0" /></td> 
    <td width="24%">&nbsp;</td> 
    <td width="10%">&nbsp;</td> 
    </tr> 
    <tr class="l_green"> 
    <td>average number of moderate-risk activity per week in the last year</td> 
    <td align="right"><input name="textfield2" type="text" class="input_b_r" id="textfield2" value=""/></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr class="l_white"> 
    <td>average number of hours of high-risk activity per week in the last year</td> 
    <td align="right"><input name="textfield3" type="text" class="input_b_r" id="textfield3" value=""/></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr class="d_green"> 
    <td colspan="4">NEXT YEAR</td> 
    </tr> 
    <tr class="l_white"> 
    <td>expected average number of hours of moderate-risk activity per week next year</td> 
    <td align="right"><input name="textfield4" type="text" class="input_b_r" id="textfield4" value=""/></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr class="l_green"> 
    <td>expected average number of hours of high-risk activity per week next year</td> 
    <td align="right"><input name="textfield5" type="text" class="input_b_r" id="textfield5" value=""/></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr class="l_white"> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr class="l_green"> 
    <td>&nbsp;</td> 
    <td colspan="2" style="padding-left:31px;"><input type="submit" name="button" id="Calculate" value="Calculate" class="button_calculate" /></td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr class="l_white"> 
    <td>&nbsp;</td> 
    <td colspan="2" style="padding-left:105px;">predicted number of instances next year</td> 
    <td><input name="textfield6" type="text" class="input_b" id="textfield6" /></td> 
    </tr> 
    <tr class="l_green"> 
    <td>&nbsp;</td> 
    <td colspan="2" style="padding-left:105px;">extra instances next year</td> 
    <td><input name="textfield7" type="text" class="input_b" id="textfield7" /></td> 
    </tr> 
</table> 

和這裏的JavaScript的:

$(document).ready(function(){ 

     $('#Calculate').click(function(){ 

      var IRRC2 = 1.35; 
      var IRRC3 = 2.75; 

      var Npast = Number($("#textfield1").val()); 
      var t2hrswk = Number($("#textfield2").val()); 
      var t3hrswk = Number($("#textfield3").val()); 
      var t2nexthrswk = Number($("#textfield4").val()); 
      var t3nexthrswk = Number($("#textfield5").val()); 

      var t2epyr = t2hrswk * 6 * 52; 
      var t3epyr = t3hrswk * 6 * 52; 
      var t01epyr = 52 * 7 * 24 * 6 - t2epyr - t3epyr; 

      var epochBaseInstances = Npast/(t01epyr + IRRC2 * t2epyr + IRRC3 * t3epyr); 

      var baselineCoefficient = Math.log(epochBaseInstances); 

      var t2nextepyr = t2nexthrswk * 6 * 52; 
      var t3nextepyr = t3nexthrswk * 6 * 52; 
      var t01nextepyr = 52 * 7 * 24 * 6 - t2nextepyr - t3nextepyr; 

      var predictedInstances = Math.exp(baselineCoefficient) * (t01nextepyr + IRRC2 * t2nextepyr + IRRC3 * t3nextepyr); 

      var roundedPredictedInstances = Math.round(predictedInstances * 10)/10; 

      var lastYearTotal = Number($("#textfield1").val()); 

      var extraInstancesRounded = Math.round((roundedPredictedInstances - lastYearTotal) * 10)/10; 

      $('#textfield6').val(roundedPredictedInstances); 
      $('#textfield7').val(extraInstancesRounded); 
     }); 
    }); 

我知道需要這麼修改此,如果第一個窗體域(textfield1的)的值是0,當用戶點擊它檢測到的Calculate按鈕,然後退出,顯示錯誤信息(例如「去年的值不能爲零」)。

到目前爲止我嘗試過的一切都打破了Javascript - 欣賞任何與如何進行協助。

+2

顯示到目前爲止你已經嘗試過的事情之一。 – Barmar 2013-04-23 02:03:36

+0

它與條件語句,警報消息和返回值的值比較非常簡單。 – Ohgodwhy 2013-04-23 02:05:12

回答

4

試試這個代碼,只是Npast decleration後:

var Npast = Number($("#textfield1").val()); 
if (Npast === 0) { 
    alert("the value for Last Year cannot be zero"); 
    return; 
} 
2

試試這個

var last_year = $("#textfield1").val(); 
if (last_year == 0) { 
    alert("The value for Last Year cannot be zero, please enter valid number"); 
    return; 
} 
2

你可以嘗試將驗證您的文本框

這裏是一個示例:

$(document).ready(function(){ 
    $('#Calculate').on('click',function(){ 
     if (!validateTextField()){ 
      alert('no zeroes'); 
     } else { 
      alert('zeroes'); 
     } 
    }); 
}); 

function validteTextField(){ 
    //$('input[id^=textfield]') --> selects all input with id having a prefix textfield 
    $('input[id^=textfield]').css('background-color','white'); 
    var flg = false; 
    $('input[id^=textfield]').each(function(){ 
     if ($(this).val() === 0 || $(this).val().length === 0){ 
      $(this).css('background-color','pink'); 
      flg = true; 
     }    
    }); 
    return flg; 
} 

jsFiddle demo

相關問題