簡體   English   中英

如何在JavaScript中基於表單字段進行實時計算?

[英]How to Make Real Time Calculations in JavaScript Based on Form Fields?

這是我第一次嘗試使用JavaScript,如果這特別麻煩,請您道歉。 我正在嘗試創建一個基本的計算器,一旦按下計算按鈕,該計算器就會根據在框1,2和3中輸入的值計算出“研究樣本”(.forthBox.value下的總和)。

到目前為止,這在js.fiddle上有效,但是在我嘗試將代碼復制到我的網站上時無效。 是否有人對造成這種情況有任何想法?

謝謝克里斯

 <script type="text/javascript"> function calc() { var form = document.getElementById('autoSumForm'); one = form.firstBox.value; two = form.secondBox.value; three = form.thirdBox.value; form.fourthBox.value = ((((two) * (two)) * 0.25) / (((three) / 100) * ((three) / 100)) / (1 + (((two) * (two)) * 0.25) / (((three) / 100) * ((three) / 100) * (one)))); } function stopCalc() { clearInterval(interval); } </script> <form id="autoSumForm"> Population Size <input class="right" type=text name="firstBox" value="" onFocus="startCalc();" onBlur="stopCalc();">Confidence Level <select name="secondBox"> <option value="1.645">90%</option> <option value="1.96">95%</option> <option value="2.576">99%</option> </select> Margin of Error <input class="right" type=text name="thirdBox" value="" onFocus="startCalc();" onBlur="stopCalc();"> <input type="button" value="Calculate" onClick="calc()" name="button">Sample Needed <input class="right" type=text name="fourthBox"> </form> 

需要為表單使用getElementById,以便您訪問其中的元素。

<script type="text/javascript">
function calc(){
    var form = document.getElementById('autoSumForm')
    one = form.firstBox.value;
    two = form.secondBox.value;
    three = form.thirdBox.value;
    form.fourthBox.value = ((((two) * (two)) * 0.25) / (((three) / 100) * ((three) / 100)) / ( 1 + (((two) * (two)) * 0.25) / (((three) / 100) * ((three) / 100) * (one))));
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM