简体   繁体   English

如何在jquery中调用函数

[英]how to call a function in jquery

I have a function called monitoring() as in this fiddle . 我有一个名为monitoring()的函数,就像这个小提琴一样 My question here is, if i would like to call that function, can I simply do this? 我的问题是,如果我想调用该函数,我可以简单地这样做吗?

$('#status_table #monitor_'+rowID).change(monitoring(rowID));

My logic is that, when the status of the checkboxes( #monitor_'+rowID is the checkbox ID) are changed, the function will be called. 我的逻辑是,当复选框的状态( #monitor_'+rowID是复选框ID)发生变化时,将调用该函数。

As i would like to find out whether the function is being called, i do an append as follow: 因为我想知道函数是否被调用,我做一个追加如下:

$('#test').append(fbType + fbNum);

But it does not show up anything for fbType and fbNum in the div #test. 但它没有为div #test中的fbType和fbNum显示任何内容。 Can someone point to me how i should call the function or correct my mistakes in that code/logic? 有人能指出我应该如何调用该函数或纠正我在该代码/逻辑中的错误?

change expects a reference to a function, whereas you're currently calling it and using its return value as the argument to change –not what you want, since it doesn't return a function. change期望对函数的引用 ,而您当前正在调用它并使用其返回值作为change的参数 - 不是您想要的,因为它不返回函数。

One option is to create a function generator that takes the row ID and use the return value as the argument to change , roughly: 一种选择是创建一个函数生成器,它接受行ID并使用返回值作为参数进行change ,大致如下:

function createMonitor(rowId) {
    return function() {
        // Do something with rowId
    };
};

$('#status_table #monitor_'+rowID).change(createMonitor(rowID));

Without knowing where rowID is coming from it's difficult to know if this is the best approach. 在不知道rowID来自何处的情况下,很难知道这是否是最佳方法。

Your current code will call the function monitoring and bind its return value as the event handler , which is probably not what you need. 您当前的代码将call函数monitoring并将其返回值绑定为事件处理程序 ,这可能不是您需要的。

You can instead do it like: 你可以这样做:

$('#status_table #monitor_'+rowID).change(function() {
    monitoring(rowID);
});

Ah the power of closures: 关闭的力量啊:

$('#status_table #monitor_'+rowID).change(function() { monitoring(rowID) });

There will be a little more to it than this, as you probably need to actually pass in rowID somewhere; 它会比这更多,因为你可能需要在某处实际传递rowID; looking at your fiddle, it seemed at first glance as though you'd want to have this snippet inside the function addText , where you calculate a rowID. 看着你的小提琴,乍一看好像你想在函数addText有这个片段,你在那里计算一个rowID。

Corrected fiddle (with rowID being utilized): http://jsfiddle.net/EVW4S/ 纠正小提琴(使用rowID): http//jsfiddle.net/EVW4S/

Seems like there are more problems with this code... One tip that might help in debugging: console.debug is useful (rather than using alert() or changing content) 看起来此代码存在更多问题...可能有助于调试的一个提示: console.debug很有用(而不是使用alert()或更改内容)

Create a button somewhere to trigger the function then. 然后在某处创建一个按钮来触发该功能。 You can have a better control over the function as well. 您也可以更好地控制该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM