0

我有数据表设置以及引导选择,一个将复选框变成开/关开关的插件。当切换开关时,我试图获取作为 ID 的输入的名称。

这是我的代码和实时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);
        });
    }
});
4

1 回答 1

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);
});

演示:小提琴

于 2013-08-28T07:10:31.647 回答