鑑於這個網站:遍歷錶行,計算(乘)輸入值,設置導致另一個輸入
<table class="hours-table">
<tr>
<th>Hours</th>
<th>Hourly Rate</th>
<th>Date Total</th>
</tr>
<tr>
<td class="hours"><input type="text" class="hours" name="hours-01" value="" /></td>
<td class="rate"><input type="text" class="rate" name="rate-01" value="" /></td>
<td class="date-total"><input type="text" class="date-total" name="date-total-01" value="" /></td>
</tr>
</table>
<p><a class="calculate" href="#" title="calculate row">Calculate</a></p>
我通過行試圖循環,讓每一行的時間和速率值,將它們相乘並在「日期 - 總計」輸入中設置該值(不一定必須是總計的輸入,但我將對多個列進行另一次計算)
抓我的頭幾個小時爲什麼千次嘗試獲得這些值不起作用,例如:
$('.calculate').on('click', function() {
$('.hours-table tr').each(function() {
var hours = $(this).find('input.hours').val(); // nope
var hours = $('input.hours', this).val(); // nope
var hours = $('input.hours', $this).val(); // nope
//var dateTotal = (hours * rate);
//$(this).find('input.date-total').val(dateTotal);
return false;
}) //END .each
}) // END click
請問,我到底在做什麼這個循環錯了?
適用於我 - http://jsfiddle.net/Lr5pq/1/。我不確定'return false'是什麼意思,但我認爲你想阻止'''的默認行爲 - 所以我將它從內部循環移出 – Ian
設置提出一個js小提琴,很快就會有答案 –
在'.each()'回調中返回false將退出循環。這可能不是你想要的。 @Ian,你應該讓你的評論成爲答案。 – jrummell