我有 ajax 交互问题
var json = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Anna", lastName: "Smith" },
{ firstName :"Peter", lastName: "Jones" }
];
$(".container").append("<ul>");
// cycle through array and select each object in it
$.each(json, function(idx, object) {
$(".container").append("<li>" + object.firstName + " " + object.lastName + '<input type="submit" value="del"><input type="hidden" value="' + idx + '"></li>');
});
$(".container").append("</ul>");
$("li").on("click", "input", function() {
var idx = $(this).next().val();
json.splice(idx, 1);
// clear container
$(".container").text("");
$(".container").append("<ul>");
// cycle through array and select each object in it
$.each(json, function(idx, object) {
$(".container").append("<li>" + object.firstName + " " + object.lastName + '<input type="submit" value="del"><input type="hidden" value="' + idx + '"></li>');
});
$(".container").append("</ul>");
这是一个小提琴
如果我单击删除,一切都会按预期工作。但是,如果我想删除第二项,则没有任何响应。
谁能向我解释为什么会这样?提前致谢!