Jquery 将onclick添加到json生成的表中
我有一个从数据库解析的JSON数组Jquery 将onclick添加到json生成的表中,jquery,json,Jquery,Json,我有一个从数据库解析的JSON数组 $.getJSON(… , 使用$迭代数据。然后将每个按名称放入表1 function(geojson) { $.each(geojson.features, function(i, feature) { var id = feature.properties.id; var name = feature.properties.name; var otherDetails = feature.prope
$.getJSON(… ,
使用$迭代数据。然后将每个按名称放入表1
function(geojson) {
$.each(geojson.features, function(i, feature) {
var id = feature.properties.id;
var name = feature.properties.name;
var otherDetails = feature.properties.otherDetails;
// Populate table1 with clickable links
$("#table1 table").append('<tr><td><a href="#" onclick="['+id+']; return false;">'+name+'</a></td></tr>')
//populate table2 with name
.click(function(){
$("#table2 table").append('<tr><td><a">'+name+' '+otherDetails+'</a></td></tr>')
})
});
});
函数(geojson){
$.each(geojson.features,function(i,feature){
var id=feature.properties.id;
var name=feature.properties.name;
var otherDetails=feature.properties.otherDetails;
//用可点击的链接填充表1
$(“#表1表”)。附加(“”)
//用名称填充表2
。单击(函数(){
$(“#table2 table”)。追加(“在table1的行中添加一个单击处理程序。单击时,拉出名称以放入table2的新行中
$('#table1 table').on('click', 'tr', function()
{
var name = $(this).find('a').text();
$('#table2 table').append('<tr><td><a">' + name + '</a></td></tr>');
});
$('table1 table')。在('click','tr',function()上
{
var name=$(this.find('a').text();
$(“#table2 table”).append(”。这将允许您也使用otherinfo数据。将在table
级别重复应用单击事件处理程序。请尝试将其应用于行,如下所示:
// Populate table1 with clickable links
var newRow = $('<tr><td><a href="#" onclick="['+id+']; return false;">'+name+'</a></td></tr>');
$("#table1 table").append(newRow);
$(newRow).click(function(){
$("#table2 table").append('<tr><td><a">'+name+' '+otherDetails+'</a></td></tr>')
});
//使用可单击的链接填充表1
var newRow=$('');
$(“#表1表”)。追加(新行);
$(新行)。单击(函数(){
$(“#表2表”)。追加('