あなたは配列の値を得ることができる場合、あなたがそれらを合計するjQueryのを使用する必要はありません。配列オブジェクトに既に存在するメソッドを使用して作業を行うことができます。
配列には.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