datatables在回调中获取行中输入的名称?

时间:2013-08-28 06:55:53

标签: jquery datatables

我有数据表设置以及bootstrap select,这是一个将复选框变为开/关开关的插件。切换开关时,我正在尝试获取输入的名称。

这是我的代码和实时jsfiddle,当我点击开关时,警报未定义。如何获取输入名称(123)以提醒?有关boostrap插件的更多信息,请访问http://www.bootstrap-switch.org/

由于

testdata = [{
    "id": "<input name\"123\" id=\"create-switch\" type=\"checkbox\">",
    "country_code": "UK",
    "title": "Legal Director",
    "pubdate": "2012-03-08 00:00:00",
    "url": "http://..."
}, {
    "id": "59",
    "country_code": "UK",
    "title": "Solutions Architect,",
    "pubdate": "2012-02-23 00:00:00",
    "url": "http://..."
}];

$('#test').dataTable({
    "aaData": testdata,
        "aoColumns": [{
        "mDataProp": "id"
    }, {
        "mDataProp": "country_code"
    }, {
        "mDataProp": "title"
    }, {
        "mDataProp": "pubdate"
    }, {
        "mDataProp": "url"
    }],
        "fnCreatedRow": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        $('#create-switch', nRow).wrap('<div class="make-switch switch-mini" />').parent().bootstrapSwitch();
    },
        "fnInitComplete": function () {
        alert("init");
        $('.make-switch').on('switch-change', function (e) {
            var postid = $('.make-switch input').attr('name');
            alert(postid);
        });
    }
});

1 个答案:

答案 0 :(得分:2)

尝试

testdata = [{
    "id": "<input name=\"123\" id=\"create-switch\" type=\"checkbox\">",
        "country_code": "UK",
        "title": "Legal Director",
        "pubdate": "2012-03-08 00:00:00",
        "url": "http://..."
}, {
    "id": "59",
        "country_code": "UK",
        "title": "Solutions Architect,",
        "pubdate": "2012-02-23 00:00:00",
        "url": "http://..."
}];

$('#test').dataTable({
    "aaData": testdata,
        "aoColumns": [{
        "mDataProp": "id"
    }, {
        "mDataProp": "country_code"
    }, {
        "mDataProp": "title"
    }, {
        "mDataProp": "pubdate"
    }, {
        "mDataProp": "url"
    }],
        "fnCreatedRow": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        $('#create-switch', nRow).wrap('<div class="make-switch switch-mini" />').parent().bootstrapSwitch();
    },
        "fnInitComplete": function () {}
});

$('#test').on('switch-change', '.make-switch', function (e) {
    var postid = $(this).find('input').attr('name');
    console.log(postid);
});

演示:Fiddle