[英]jQuery code does not fire on modal forms
I have a simple link that will fire an ajax call and change a status, and I want that new status reflected (instantly) on the modal form. 我有一个简单的链接,它将触发ajax调用并更改状态,我希望在模态窗体上反映(立即)新状态。
Problem is: when I click the link that is on the modal form (below), nothing happens. 问题是:当我点击模态表格(下面)上的链接时,没有任何反应。
<a title="Autoplay" data-placement="top" data-toggle="tooltip" class="glyphicon glyphicon-stop" id="autoplayStatus" href="#"></a>
In $(document).ready(), I have this: 在$(document).ready()中,我有这个:
$("#autoplayStatus").click(function(event){
alert('test');
})
Is there something I should do to ensure that jQuery can select and bind to objects on a modal form? 我应该做些什么来确保jQuery可以选择并绑定到模态窗体上的对象?
To ensure that jQuery binds to buttons (or other stuff) in modals created by Bootstrap, you should write your event handler like this: 为了确保jQuery绑定到Bootstrap创建的模式中的按钮(或其他东西),您应该像这样编写事件处理程序:
$(document).ready(function() {
$('.container').on('click', '#autoplayStatus', function() {
alert('Ohai!');
});
});
and then, in my example, wrap the anchor tag in a container: 然后,在我的示例中,将锚标记包装在容器中:
<div class="container">
<a title="Autoplay" data-placement="top" data-toggle="tooltip" class="glyphicon glyphicon-stop" id="autoplayStatus" href="#"></a>
</div>
On a sidenote, it's better practice to use <button>
instead of <a>
when creating, well, a button. 在旁注中,最好在创建按钮时使用
<button>
代替<a>
。 =) =)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.