【发布时间】:2018-08-29 21:03:15
【问题描述】:
我试图为车把模板中的按钮提供点击事件。但这些事件并没有被解雇。请在下面找到代码:
模板
<table class="table shadow p-3 mb-5 bg-white rounded">
<thead class="thead-dark">
<tr>
<th scope="col">NAME</th>
<th scope="col">EMAIL</th>
<th scope="col">DOB</th>
<th scope="col">DEPT</th>
<th scope="col">GENDER</th>
<th scope="col">AGE</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{{#each ALL_RECORDS}}
<tr>
<td scope="row">{{this.name}}</td>
<td scope="row">{{this.eMail}}</td>
<td scope="row">{{this.DOB}}</td>
<td scope="row">{{this.dept}}</td>
<td scope="row">{{this.gender}}</td>
<td scope="row">{{this.age}}</td>
<th scope="row" style="text-align: center"><button class="btn btn-primary" type="button" name="EDIT_BTN">EDIT</button></th>
<th scope="row" style="text-align: center"><button class="btn btn-danger" type="button" name="DEL_BTN">DELETE</button></th>
</tr>
{{/each}}
</tbody>
</table>
jQuery
>$("[name='DEL_BTN']").click(function(){
alert("DELETE BUTTON CLICKED");
})
点击删除按钮时,它不会触发警报。 从模板移出到 HTML 页面时,相同的按钮工作正常。你能帮我解决这个问题吗??
【问题讨论】:
-
在绑定事件时您的按钮可能不存在尝试从不是由把手生成的父元素委托事件,例如:$('.table').on(' click', 'button' , function(e){ //do stuff });
-
@digital-pollution 这行得通。非常感谢……
标签: jquery html handlebars.js