0

以下の関数では、ID の最大カウントを見つけてから 1 を追加しています。DOM に追加された新しい値が表示されないことを除いて、すべてが機能します。したがって、関数を 2 回目に呼び出しても増加しません。

新しい値を DOM に正しく追加していませんか?

function addEdu(){
    var currentCount = $('input.uniqueId').val();
    currentCount = Math.max(currentCount);
    var nextCount = parseInt(currentCount) + 1;
    var newEdu = "<input type='hidden' name='fieldId[]' value='" + nextCount + "' class='uniqueId' /><p class='dual'><input type='text' name='educationTitle[]' "; //Shortened for clarity
    $("#eduHistory").append(newEdu); 
    $( ".datepicker" ).datepicker();
}
4

2 に答える 2

1

次を決定しようとしている方法.uniqueIdは間違っています。
まず$("input.uniqueId").val()、選択範囲の最初の要素の値が表示されます。
2番目の問題は、すでに述べたように、最初の要素の値であるMath.max(currentCount)値を常に返すということです。currentCount

これは意図したとおりに機能するはずです。

// get an array with all uniqueIds
var currentIds = $("input.uniqueId").map(function() {
    return parseInt($(this).val(), 10);
});

// determine the biggest uniqueId
var currentCount = Math.max.apply(Math, currentIds);

// next uniqueId (if there is no "input.uniqueId" then currentCount will be -Infinity)
var nextCount = (currentCount === -Infinity ? 0 : currentCount) + 1;

// ...
于 2013-04-02T21:27:14.643 に答える