テンプレートのコピーを作成し、.click()メソッドをそれらに適切に結び付けるときに問題があるようです。たとえば、次のJavaScriptを使用します。
function TestMethod() {
var test = Array();
test[0] = 0;
test[1] = 1;
test[2] = 2;
// Insert link into the page
$("#test_div").html("<a href=\"#\"></a><br>");
var list;
for (x = 0; x < test.length; x++) {
var temp = $("#test_div").clone();
temp.find('a').html("Item #" + test[x]);
temp.click(function () { alert(x); });
if (list == undefined)
list = temp;
else
list = list.append(temp.contents());
}
$("#test_div2").append(list);
}
これで私が見ている問題は、ユーザーがどのアイテムをクリックしても、最初のいくつかのアイテムをクリックした場合でも、常にalert(2)を実行することです。
どうすればこれを機能させることができますか?
編集:私は問題をはるかに明確に示すはずの非常に単純な例を作成しました。どのアイテムをクリックしても、常に番号2のアラートボックスが表示されます。