[英]Javascript bootstrap modal calling function
My code is like below: 我的代码如下:
function OnApply(runFunc) {
$("#myCfmModal").modal('show');
$("#myCfmModal").find("#myCfmModalText").html("Apply change now?");
$("#myCfmModal").find("#myCfmModalHeader").css("background","#cccccc");
$("#myCfmModal").one('click', '#okbtn', runFunc);
}
function cool() {
alert("cool!");
}
I have a problem running the code above. 我在运行上面的代码时遇到问题。 I call the OnApply(cool())
as below: 我将OnApply(cool())
调用如下:
<button class="btn btn-success" onclick="OnApply(cool())">Apply</button>
OnApply(cool())
will show a modal box and run a function cool()
. OnApply(cool())
将显示一个模态框并运行一个函数cool()
。 What I want to do is to run function cool()
when I click the OK button in the modal box. 我想要做的是当我单击模态框中的确定按钮时运行函数cool()
。 However the cool()
function will always run first before I click the OK button. 但是,在单击“确定”按钮之前, cool()
函数将始终先运行。 Any idea what went wrong? 知道出了什么问题吗?
The issue is because you are passing the result of the cool()
function to OnApply()
- you instead need to pass the reference to the function by removing the trailing brackets. 问题是因为您将cool()
函数的结果传递给OnApply()
- 您需要通过删除尾部括号来传递对函数的引用。 Try this: 尝试这个:
<button class="btn btn-success" onclick="OnApply(cool)">Apply</button>
You can call runFunc
using " click
" JavaScript event. 您可以使用“ click
”JavaScript事件调用runFunc
。
Try this. 尝试这个。
function OnApply(runFunc)
{
$("#myCfmModal").modal('show');
$("#myCfmModal").find("#myCfmModalText").html("Apply change now?");
$("#myCfmModal").find("#myCfmModalHeader").css("background","#cccccc");
$("#myCfmModal").find('#okbtn').click(functions(){
runFunc();
});
}
function cool()
{
alert("cool!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.