2013-08-28 61 views
0

我有datatables設置以及引導選擇,一個插件將複選框變成開啓/關閉開關。當開關切換時,我試圖獲取輸入的名稱是一個ID。datatables從回調中點擊行獲取輸入的名稱?

這是我的代碼和生活jsfiddle當我點擊開關警報未定義。我如何獲得輸入名稱(123)以提醒?而關於自舉更多的信息在這裏插件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); 
     }); 
    } 
}); 

回答

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

+0

在我的控制檯盯着我不知道我失蹤的=標誌。感謝您更新的小提琴! – Anagio

+1

@ArunPJohny只是想把你的注意力集中到Anagio糾正的錯誤之一:'testdata = [{「{」輸入名稱\「123 \」'你缺少'='符號解決方法是:'testdata = [{「{」}「 noamtcohen

+0

@noam yes ...我已將它固定在小提琴中......但忘了在此提及它..謝謝 –