在Parse.com Query中循环时添加DOM addEventListener

时间:2015-11-16 17:32:09

标签: javascript dom parse-platform onclick addeventlistener

我决定将我的(不那么复杂的)应用程序从Firebase.com移到Parse.com。所以我之前在DOM中使用的代码片段从昨天开始就可以动态创建N个表单,但不能正确地传递参数。

这里是我的片段(我剪掉了很多重复的词条):

function displayForms() {
	while (divBot.hasChildNodes()) {
			divBot.removeChild(divBot.lastChild);
	}
	query.find({
	success: function(results) {
		for (var i = 0; i < results.length; i++) {
			// Ok suppose that my results was something like
			// Position 0. ID = "Tom" and other things
			// Position 1. ID = "Mark" and other things
			// Position 2. ID = "Ivan" and other things
			
			var object = results[i];
			var id = object.id;
			
			var form = document.createElement("FORM");
			var fieldset = document.createElement("FIELDSET");
			var inputId = document.createElement("INPUT");
			inputId.setAttribute("type", "hidden");
			inputId.value = id;
			fieldset.appendChild(inputId);
			
			var buttonDel = document.createElement("INPUT");
			buttonDel.setAttribute("type", "button");
			buttonDel.value = "Elimina";
			console.log(id);
			// HERE i have "Tom" at first iterate
			// "Mark" at second and "Ivan" at third
			// I also printed "i" and i have 0, 1, 2
			buttonDel.addEventListener("click", function() {
				// HERE i have "Ivan" all the times
				// I also printed "i" and i have always 2
				// So i am passing always the last value
				del(inputId.value);});
			
			fieldset.appendChild(buttonDel);
			form.appendChild(fieldset);
			divBot.appendChild(form);
			}
	},
	error: function(error) {
	alert("Error: " + error.code + " " + error.message);}
	});
}

0 个答案:

没有答案