0

我正在使用以下教程作为指南在 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);
}
4

1 回答 1

0
var list = $('<ul data-split-icon="some_icon"></ul>');

var html = ''; for (i = 0; i <= singleArray.length - 1; i++) {

var param1 = "'"+ dataArray[i] +"'";
html += '<li><a href="#" onclick="clickHandler1('+param1+')"></a>Button 1<a href="#" onclick="clickHandler2('+param1+')"></a>Button 2 </li>';

}

list.append($(html));

$('<ul>').trigger('create');

function clickHandler1(e){

警报(e);

}

于 2012-05-23T02:39:00.900 回答