我試圖轉換直javacript功能火與jQuery。點擊簡單的覆蓋caculator不會點擊並且沒有錯誤。 jQuery和straigt JS
我沒有得到任何輸出錯誤但它不會上的jsfiddle進行計算。
var form = document.product_form_top;
function process(v) {
var value = parseInt(document.getElementById('item_qty').value);
value += v;
document.getElementById('item_qty').value = value.toFixed(0);
}
function calculate() {
form.cubicYards.value = (form.length.value/3) * (form.width.value/3) * (form.depth.value/35);
document.getElementById('yards').innerHTML = finaloutput
var number = parseFloat(form.item_qty.value);
var finaloutput = number.toFixed(0);
form.item_qty.value = form.cubicYards.value * 14.7; //calculate yards
var number = parseFloat(form.cubicYards.value);
var finaloutput = number.toFixed(0);
document.getElementById('item_qty').innerHTML = finaloutput
form.weightPounds.value = (form.length.value/3) * (form.width.value/3) * (form.depth.value * 21);
form.weightPounds.value * 700;
var number = parseFloat(form.weightPounds.value);
var finaloutput = number.toFixed(0)
document.getElementById('pounds').innerHTML = finaloutput
}
這裏有一個的jsfiddle http://jsfiddle.net/4L4St/8/
這是一個使用的onclick =「計算()的一個工作版本;一個按鈕執行此功能,我寧願使用jQuery的。點擊功能來啓動它
http://spartonenterprises.com/store/playground-mulch
而且,我使用toFixed(0),以去除多餘的小數點和Firebug的檢查,當它看起來工作,但可見的HTML顯示小數。奇怪?
你應該真的需要花一些時間在[DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself)這個代碼上。它會幫助你和所有想要回答的人。看看簡單的更改可以在代碼的可讀性上做些什麼:https://gist.github.com/elclanrs/5315134。加上適當的縮進和間距。 – elclanrs 2013-04-04 23:02:10
謝謝你。我會盡量更好地格式化它。 – 2013-04-04 23:07:45
刪除行'function calculate(){'及其對應的'}' - 該函數永遠不會被調用;您需要將該函數的內容直接放在您傳遞給'.click()'的匿名函數中。然後你的代碼會在你點擊按鈕時運行,如下所示:http://jsfiddle.net/nnnnnn/4L4St/2/,你可以開始修復控制檯中出現的實際錯誤(比如'Uncaught TypeError:Can not read未定義'的屬性'cubicYards')。 – nnnnnn 2013-04-04 23:07:54