在模式窗口(Bootstrap)中单击按钮不触发 [英] Button click not firing in modal window (Bootstrap)

查看:91
本文介绍了在模式窗口(Bootstrap)中单击按钮不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模式窗口中有一个按钮:

I have a button in a modal window:

<button id="submit_btn" class="btn btn-link">Send the invitation</button>

我正在尝试捕获点击:

$('#submit_btn').click(function(event){
    event.preventDefault();
    alert( "GO" ); 
});

我已经阅读到在模式窗口中会吞下按钮单击,为防止这种情况,请使用event.preventDefault().但这是行不通的.我无法捕获此按钮的click事件.

I've read that button clicks get swallowed in modal windows and that, to prevent that, use event.preventDefault(). But that's not working. I can't capture the click event of this button.

我还想念什么?

任何提示都将不胜感激!

Any tips are greatly appreciated!

推荐答案

尝试一下-

$(document).on("click", "#submit_btn", function(event){
    alert( "GO" ); 
});

或者这个-

$(document).delegate("#submit_btn", "click", function(event){
    alert( "GO" ); 
});

如果您使用的是旧版jQuery,则可能必须使用live方法.

If you are using an older version of jQuery you may have to use the live method instead.

实时方法(仅在必要时使用)

Live method (use this only if you have to)

$("#submit_btn").live("click", function(event){
    alert( "GO" ); 
});

我相当确定上述三种方法之一可以解决您的问题.您的事件处理程序不起作用的原因是,在评估事件处理程序时,您的submit_btn元素不存在.我为您提供的上述3个处理程序将处理现在和将来存在的Submit_btn元素.

I'm fairly certain that one of these 3 methods above will solve your issue. The reason your event handler doesn't work is because your submit_btn element doesn't exist at the time your event handler is evaluated. The above 3 handlers I gave you will work on a submit_btn element that exists now and in the future.

/编辑

这是一个有效的jsfiddle- http://jsfiddle.net/GqD4f/8/

Here is a jsfiddle that works - http://jsfiddle.net/GqD4f/8/

/另一个修改

我使用您帖子中的方法制作了一个jsfiddle,它起作用了- http://jsfiddle.net /CR3bM/1/

I made a jsfiddle using the approach you had in your post and it works - http://jsfiddle.net/CR3bM/1/

您是否未将事件处理程序置于DOM中?

Are you not putting your event handler in DOM ready?

这篇关于在模式窗口(Bootstrap)中单击按钮不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆