【发布时间】:2017-08-04 12:09:18
【问题描述】:
我想用一个能够创建新输入文本字段的按钮和一个使用 jquery 的按钮制作一个表单。然后我新生成的按钮有问题,无法调用jquery动作。
$(document).ready(function(){
$('.add').click(function () {
$('#more').append('<input type=text />');
$('#more').append('<button type="button" class="btn btn-danger remove"><i class="fa fa-minus-circle"></i></button>');
});
$('.remove').click(function(){
alert('this text field should be removed');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-primary add" type="button"><i class="fa fa-plus-circle"></i></button>
<div id="more"></div>
当add 按钮被点击时,它会附加新的输入文本和按钮。但是这个带有remove 类的新按钮在单击时无法触发。任何人都可以在这里找到我想念的东西吗?
【问题讨论】:
-
删除点击事件处理程序在代码执行时应用,大概是在您点击添加按钮之前。由于在执行时新按钮不存在,因此事件处理程序未绑定到它们。
-
使用
$(.'remove').on('click', ...,这应该可以。了解直接和委托事件here。
标签: javascript jquery html