我想了解它是如何工作的,爲什麼它只更新我正在處理的輸入。jquery更改每個只更新一個輸入
我有以下3個輸入:
<div class="row member">
<div class="input-field col s4 nogap">
<input id="money" type="text" value="0">
</div>
<div class="input-field col s4 nogap">
<input id="hours" type="number" step="any" class="validate" value="0">
</div>
<div class="input-field col s4 nogap">
<input id="name" type="text" class="validate">
</div>
</div>
及以下JS:
$(document).ready(function() {
var totalHours = 0;
var totalwHours = 0;
var money = 0;
var perHours = 0;
var wHours = [0, 0 , 0 ,0 , 0 ,0];
var wMoney = [0, 0 , 0 ,0 , 0 ,0];
$('div.member').each(function(index, obj){
$(this).change(function(){
myH = $(this).find('#hours').val();
wHours[index] = myH;
var totalHours = 0;
for (var i = 0; i < wHours.length; i++) {
totalHours += wHours[i] << 0;
}
perHours = money/totalHours;
wMoney[index] = perHours * myH;
$(this).find('#money').val(wMoney[index]);
});
});
});
這一切的偉大工程,變量正確更新,但例如,如果我的工作部件2並改變他的小時數,只有他的「金錢」輸入更新,另外兩個保持不變,直到我改變他們的「小時」字段。
我真的想uderstand如何與.change
和.each
共同努力