[英]jQuery onclick Event call a function
I have the following code: 我有以下代码:
function hvcm_wait() { waitingDialog.show('Please wait while your VM is rebooting...'); } $('#hv_ConfirmShutDown').on('show.bs.modal', function(e) { $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="btn btn-danger btn-ok">Shutdown Now</a>
How do I call the javascript function hvcm_wait() when .btn-ok is clicked is clicked in Modal? 在Modal中单击.btn-ok时,如何调用javascript函数hvcm_wait()?
Any help would be highly apperciated. 任何帮助将是高度赞赏的。 Thanks! 谢谢!
Assuming that .btn-ok
is not present when script
is loaded, so use event-delegation. 假定在加载script
时不存在.btn-ok
,所以请使用事件委托。
Add these lines after hvcm_wait()
function 在hvcm_wait()
函数之后添加这些行
$('#hv_ConfirmShutDown').on('click', '.btn-ok', function() {
hvcm_wait();
})
Or if it is present in DOM then you can directly do this 或者,如果它存在于DOM中,则可以直接执行此操作
$('.btn-ok').click(function() {
hvcm_wait();
})
或者您可以将其直接添加到HTML中,如下所示:
<a class="btn btn-danger btn-ok" onclick="hvcm_wait()">Shutdown Now</a>
I would use this, first in the $() you search for the class btn-ok, then you hand it a onClick handler that executes the function. 我会用它,首先在$()中搜索btn-ok类,然后将其交给执行函数的onClick处理程序。
$(".btn-ok").click(function(){
hvcm_wait();
});
You are adding href attribute here. 您要在此处添加href属性。 First you need to prevent the href and call the normal function. 首先,您需要阻止href并调用正常函数。
$(".btn-ok").click(function(event){
event.preventDefault();
hvcm_wait();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.