, 당신은 그 합계를 jQuery를 사용할 필요가 없습니다 :
<input type="text" id="unitprice_1">
<input type="text" id="unitprice_2">
<input type="text" id="unitprice_3">
그런 다음 합계를 계산합니다. 배열 객체에 이미있는 메소드를 사용하여 작업을 수행 할 수 있습니다.
배열에는 .reduce() 메소드가 있습니다. 문서 : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce
Array.reduce는 누적 기 콜백의 역할을하는 인수로 함수를 받아들입니다. accumulator 함수는 4 개의 인수 (previousValue, currentValue, index, array)를 허용합니다. 당신은 그 중 2 개만 합쳐야합니다. 그 2 개의 인수는 previousValue와 currentValue입니다.
var sampleArray = [1, 2, 3, 4, 5];
var sum = sampleArray.reduce(function(previousValue, currentValue){
return currentValue + previousValue;
});
대상 환경이 링크 된 MDN 문서에 정의 된 프로토 타입 정의를, ECMAScript를 5 이상 추가를 지원 사용하지 않는 호환성 문제에 직면하는 경우. (아래에 추가)
if (!Array.prototype.reduce) {
Array.prototype.reduce = function reduce(accumulator){
if (this===null || this===undefined) throw new TypeError("Object is null or undefined");
var i = 0, l = this.length >> 0, curr;
if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
throw new TypeError("First argument is not callable");
if(arguments.length < 2) {
if (l === 0) throw new TypeError("Array length is 0 and no second argument");
curr = this[0];
i = 1; // start accumulating at the second element
}
else
curr = arguments[1];
while (i < l) {
if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this);
++i;
}
return curr;
};
}
ID는 ** 고유해야합니다 **! (그리고 가능하다.) –
'id' 속성 대신'class' 속성을 사용하십시오. (페이지에서 동일한 ID를 가진 요소를 두 개 이상 가질 수 없습니다.) –
ID를 고유하게 만들면 확인합니다. 어떻게 추가 할 수 있습니까? Add new row를 클릭하면 unitprice가 동적이라는 것을 기억하고 또 다른 unitprice를 생성합니다. 그래서 나는 그들의 합이 필요합니다. – Faizan