2013-01-14 55 views
0

我有一个jQuery模板(https://github.com/jquery/jquery-tmpl)和一个包含两个列表的Object()。jQuery模板,读取数据点击按钮tmplItem()

<!-- HTML --> 
<table> 
     <thead> 
      <tr> 
       <th> 
        Some value 
       </th> 
       <th> 
        A Button 
       </th> 
      </tr> 
     </thead> 
     <tbody id="results-body"> 
     </tbody> 
    </table> 
<!-- HTML --> 

var templateHTML = "{{each list1FromTemplate}}<tr><td>{{= Value}}</td><td><input type='button' name='alert' value='Alert' class='alert'/></td></tr>{{/each}}"; 

var results = new Object(); 
    results.list1 = list1Data; //Retrieved from an ajax call 
    results.list2 = list2Data; //Retrieved from an ajax call 

$.tmpl(templateHTML, { list1FromTemplate: results.list1, list2FromTemplate: results.list2 }).appendTo("#results-body"); 

//Here goes the question: 

$(".alert").click(function() { 

alert(
//Alert the value from {{= Value}} in the same row 
); 

}); 

一切工作正常,除了与类警报

如何从数值数据“{{=值}}” 在同一行在当带班“警报”按钮是按钮点击???

UPDATE:

即使世界的方式检索使用tmplItem()或东西的价值?

+0

给了TD类/ ID或在跨度类/ ID包的价值,然后使用jQuery来访问它,比如'$(本) .closest( 'TR')。找到( '价值包装')。文本()' –

回答

0

给td一个class/id或者用class/id包装这个值,然后用jquery来访问它,比如alert($(this).closest('tr').find('.value-wrapper').text())(评论提升为答案)。

另一个选项是设置就是BEING点击输入属性(例如数据值),你会再与alert($(this).attr('data-value'));

0

访问给ID为每个TD(可能是循环计数器的值ID),并调用此类报警功能:

$(.alert).click(function(){ 
    $('#td_id').val(); 
});