2014-01-16 63 views
0

我是JavaScript新手,我試圖創建一個看似簡單的表單。但由於某種原因,我無法讓它工作。我知道這對入門級javascripters來說應該是一件容易的事,但我無法弄清楚。我今天已經嘗試了很多解決方案,但是我一直無法實現它。任何幫助你們能給將不勝感激:)用於計算學期課程費用的Javascript表格

我試圖建立一個估計翹課成本的形式:

http://jsfiddle.net/Gt2vK/2/

function calculateCost() { 
    // enter annual tuition 
    var $annualTuition = parseInt($('#annual_tuition').val(), 10); 
    // tuition per semester 
    var semesterTuition = $annualTuition/3; 
    // total number of credits for semester 
    var $semesterCredits = parseInt($('#semester_credits').val(), 10); 
    // cost of a single credit 
    var singleCreditCost = semesterTuition/$semesterCredits; 
    // total credits for class being skipped 
    var $skippedTotalCredits = parseInt($('#skipped_total_credits').val(), 10); 
    // total cost for class being skipped 
    var skippedTotalCreditsCost = $skippedTotalCredits * singleCreditCost; 
    // total number of days in semester for class being skipped 
    var $skippedTotalDays = parseInt($('#skipped_total_days').val(), 10); 
    // (total cost of class)/(total number of class days in semester) = cost of class 
    var skippedSingleClassCost = skippedTotalCreditsCost/$skippedTotalDays; 
}​ 

$(function(){ 

    $('#cost').on('click', function(){ 
     $('#costTotal').html(calculateCost); 
     // why the heck doesnt this work?!? 
     alert(calculateCost); 
    }); 

}); 

和我的HTML :

<form id="costForm" action="#" onsubmit="#"> 

    <div> 
     <label for="annual_tuition">Annual Tuition:</label> 
     <input type="text" name="annual_tuition" id="annual_tuition" value="" tabindex="1"> 
    </div> 

     <div> 
     <label for="semester_credits">Semester Credits</label> 
     <input type="text" name="semester_credits" id="semester_credits" value="" tabindex="1"> 
    </div> 

    <div> 
     <label for="skipped_total_credits">Skipped Total Credits</label> 
     <input type="text" name="skipped_total_credits" id="skipped_total_credits" value="" tabindex="1"> 
    </div> 

     <div> 
     <label for="skipped_total_days">Skipped Total Days</label> 
     <input type="text" name="skipped_total_days" id="skipped_total_days" value="" tabindex="1"> 
    </div> 

    <div> 
     <button id="cost" type="button" >Calculate</button> 
    </div> 

    <div id="costTotal"></div> 

</form> 
+2

1)我沒有看到'calculateCost'被調用的任何地方。 2)這些變量只存在於函數的範圍內,所以它們不能在外部訪問。如果你需要函數的值,你應該調用函數並返回值。我可能錯過了什麼? – Zhihao

+1

請勿嘗試在「提交」按鈕上觸發您的onclick事件。使用按鈕按鈕。 –

+0

doh!對不起(我不知道我在做什麼) –

回答

1

你onclick事件絲打不正確,我已經改變了它:

<div> 
     <button id="cost" type="button">Calculate</button> 
</div> 

你的JavaScript更改:

function calculateCost() { 
// enter annual tuition 
var $annualTuition = parseInt($('#annual_tuition').val(), 10); 
// tuition per semester 
var semesterTuition = $annualTuition/3; 
// total number of credits for semester 
var $semesterCredits = parseInt($('#semester_credits').val(), 10); 
// cost of a single credit 
var singleCreditCost = semesterTuition/$semesterCredits; 
// total credits for class being skipped 
var $skippedTotalCredits = parseInt($('#skipped_total_credits').val(), 10); 
// total cost for class being skipped 
var skippedTotalCreditsCost = $skippedTotalCredits * singleCreditCost; 
// total number of days in semester for class being skipped 
var $skippedTotalDays = parseInt($('#skipped_total_days').val(), 10); 
// (total cost of class)/(total number of class days in semester) = cost of class 
var skippedSingleClassCost = skippedTotalCreditsCost/$skippedTotalDays; 
return skippedSingleClassCost ; 
}​ 
    $("#cost").click(function() 
     { 
     $('#costTotal').html(calculateCost()); 
    //  alert(calculateCost()); 
     } 
    ); 
+0

我在這裏嘗試了你的建議,但也許我做了錯誤的事情:http://jsfiddle.net/Gt2vK/4/ –

+0

是的,我也在玩它..另一件事是你的計算功能需要返回一個值。 –

+0

由於某些原因,我無法獲取按鈕來觸發該方法。 –

1

變化calculateCost()來return什麼會一直skippedSingleClassCost然後改變你引用skippedSingleClassCostcalculateCost()

粘貼到您jsFiddle

function calculateCost() { 
    // enter annual tuition 
    var $annualTuition = parseInt($('#annual_tuition').val(), 10); 
    // tuition per semester 
    var semesterTuition = $annualTuition/3; 
    // total number of credits for semester 
    var $semesterCredits = parseInt($('#semester_credits').val(), 10); 
    // cost of a single credit 
    var singleCreditCost = semesterTuition/$semesterCredits; 
    // total credits for class being skipped 
    var $skippedTotalCredits = parseInt($('#skipped_total_credits').val(), 10); 
    // total cost for class being skipped 
    var skippedTotalCreditsCost = $skippedTotalCredits * singleCreditCost; 
    // total number of days in semester for class being skipped 
    var $skippedTotalDays = parseInt($('#skipped_total_days').val(), 10); 
    // (total cost of class)/(total number of class days in semester) = cost of class 
    return skippedTotalCreditsCost/$skippedTotalDays; 
} 

$(function(){ 

    $('#cost').on('click', function(){ 
     $('#costTotal').html(calculateCost()); 
     // why the heck doesnt this work?!? 
     alert(calculateCost()); 
    }); 

}); 
+0

你好,我試過你的解決方案 - http://jsfiddle.net/Gt2vK/5/,任何想法爲什麼它不會工作? –

+0

不知道你的錯在哪裏,但只是複製上面的JavaScript,它似乎在這裏工作http://jsfiddle.net/Gt2vK/6/ – Tom

+0

許多基本錯誤:(檢查這個=> http:// jsfiddle。 net/Gt2vK/8/ – StupidDev