我正在使用以下教程作为指南在 jQuery mobile 中创建拆分按钮列表。我需要将数据和处理程序绑定到每个按钮(在给定的列表元素内)。下面我概述了三种方法,但似乎都不起作用。谁能告诉我如何为拆分按钮列表绑定数据?
function createSingleList(dataArray) {
var i;
var list = $("<ul></ul>");
for (i = 0; i <= singleArray.length - 1; i = i + 1) {
var button1 = $('<div><a id="button1" href="">Button 1</a><div>');
var button2 = $('<div><a id="button2" href="">Button 2</a><div>');
//Approach 1
button1.data("data", dataArray[i])
button2.data("data", dataArray[i])
//Approach 2
$("#button1").bind("click", {
data: dataArray[i]
}, clickHandler1);
$("#button2").bind("click", {
data: dataArray[i]
}, clickHandler2);
//Approach 3
$("#button1").on("click", {
data: dataArray[i]
}, clickHandler1);
$("#button2").on("click", {
data: dataArray[i]
}, clickHandler2);
var listElement = $('<li></li>').html($(button1.html() + button2.html()));
list.append(listElement);
}
}
function clickHandler1(e) {
alert(e.data);
}
function clickHandler2(e) {
alert(e.data);
}