在手柄模板中的js事件
问题描述:
当我点击一个按钮,我的模板谁有一个表是feeded。在手柄模板中的js事件
我想要做一个动作,当我双击一个表格的一行在车把模板。
<body>
<script id="lodgerSearchResult-template" type="text/x-handlebars-template">
<div class="span12">
<table id="lodgerSearchResultTable" data-show-header="true" class="table" data-toggle="table" data-height="250">
<thead>
<tr>
<th>#</th>
<th>Nom</th>
<th>Date de naissance</th>
<th>N.A.S.</th>
<th>N.A.M.</th>
<th>Immeuble</th>
</tr>
</thead>
<tbody>
{{#each this}}
<tr>
...
...
</tr>
{{/each}}
</tbody>
</table>
</script>
</body>
<script>
$('#lodgerSearchResultTable > tbody').dblclick(function() {
alert($(this).text());
});
</script>
其实没有什么事情发生。
编辑:我把双击事件在阿贾克斯成功答案
$('#lodgerSearch').on('click', function (e) {
var searchParam = $('#srch-term').val();
var url = "http://localhost:8080/lodgers/search";
if (searchParam != "") {
url = "http://localhost:8080/lodgers/search?searchTemp=" + searchParam;
}
jQuery.ajax({
type: "GET",
url: url,
success: function (data, status, jqXHR) {
$("#lodgerSearchResult").empty().append(template(data));
// is the line i added
$('#lodgerSearchResultTable > tbody').dblclick(function() {
alert($(this).text());
});
},
error: function (jqXHR, status) {
// error handler
alert("error " + jqXHR + " - " + status);
}
});
// e.preventDefault();
});
有一个更清洁的方式做同样的事情?
答
我的意思是:
<script>
alert('table before: ' + String($('#lodgerSearchResultTable')));
$(document).ready(function() {
alert('table after: ' + String($('#lodgerSearchResultTable')));
$('#lodgerSearchResultTable > tbody').dblclick(function() {
alert($(this).text());
});
});
</script>
其他非透明溶液
<script>
$(document).ready(function() {
$('#lodgerSearchResult').dblclick(function (event) {
console.log(event.target);
var res_table = $('#lodgerSearchResultTable > tbody');
if(res_table) {
alert(res_table.text());
}
});
});
</script>
+0
模板还没有反馈...就像我说的,模板是int页面,用户点击按钮搜索某个东西,Ajax调用完成,模板呈现。如果用户点击表格的一行,我想要做一个动作:我发布了大部分代码:https://jsfiddle.net/4L4b8LLc/ –
+0
好吧,我知道了...将编辑我的帖子...但我发现解决方案丑陋...如果有人在js中有更好的解决方案... –
测试代码用'的console.log($( '#lodgerSearchResultTable'));'在所述第二脚本的beginig块。看起来这个表格现在还没有创建。当[$(document).ready()](https://learn.jquery.com/using-jquery-core/document-ready/) – befzz
确定事件处理程序确定我点击了我的按钮,模板已加载...点击了一排桌子......没有任何事情发生,用你的控制台日志,我有一行表...我把控制台我的代码关于dblclick和那工作...所以不知道我在哪里需要把这个代码...让它正常工作。 –