2011-08-30 58 views
0

我有要求在jQuery中显示动态表。在点击表格数据时,值应该在下面的文本字段中设置。jquery ui动态表onclick事件不起作用

我可以知道如何从动态表捕捉名称并设置成文本字段

$(document).ready(function() { 
    //Retrieve the JSON data from the server using AJAX 
    $('#AJAXButton').click(function() { 
     $.getJSON('ajax/ajaxtest.js', function(data) { 
      processJSON(data); 
     }); 
    }); 

    //Process and display the JSON data 
    function processJSON(data) { 
     var output = '<table><tr><th>Name</th><th>Platform</th></tr>'; 
     //Loop through the Languages 
     $(data.Languages).each(function(index, element) { 
      output += '<tr><td class="clickable">' + element.Name + '</td>' + 
       '<td class="clickable">' + element.Platform + '</td></tr>'; 
     }); 
     output += '</table>'; 
     $('#AJAXDiv').html(output); 
    } 

    $("tr.clickable").live("click", function() { 
      $("#name").append(?); 
    }); 

}); 


<div id="AJAXDiv" style="width:400px; height:600px; background-color:#ddd; border:1px solid black"> 
</div> 

<div> 
    <label for="name">Created by: </label> <input id="name" /> 
</div> 

回答

0

看看this小提琴,让我知道,如果这不是你的原意。

0

你说你希望事件发生在单击行时,所以你只需将'可点击'类放在你的行而不是每个'td'。然后,点击处理程序可以访问该行内的第一个'td'(您之后的名称)。

即。 Demo

+0

这对我来说非常适合。感谢您指出我的错误 – dan

+0

没问题!如果这是你以后的事情,请标记为答案! =) –