简体   繁体   English

如何使这个jquery代码激活函数本身,而不是绑定到一个按钮来激活该函数?

[英]How to make this jquery code fire the function itself instead of binding to a button to fire the function?

How to make this jquery code fire the function itself instead of binding to a button to fire the function? 如何使这个jquery代码激活函数本身,而不是绑定到一个按钮来激活该函数?

self.ShowWindow= { observable: ko.observable(false) };
self.OnToggleShowWindow = (it) => () => it.observable(!it.observable());
$('#showWindowButton').click(self.OnToggleShowWindow (self.ShowWindow));

Right now I have to create a button and bind it with my code to be able to trigger a function in my react component. 现在我必须创建一个按钮并将其与我的代码绑定,以便能够在我的react组件中触发一个函数。 What I'm wondering is that, is it possible to just trigger the function without halving to trigger the jquery click? 我想知道的是,是否可以在不减半的情况下触发函数来触发jquery点击?

My ViewModel in the javascript component. 我的ViewModel在javascript组件中。

$ViewModel = function (element) {
    var self = this;

    self.ShowWindow= { observable: ko.observable(false) };
    self.OnToggleShowWindow = (it) => () => it.observable(!it.observable());
    self.OnToggleShowWindow (self.ShowWindow);
    $(document).ready(function () {
        self.OnToggleShowWindow(self.ShowWindow)
    };

    self.ShowDialog = function (componentId, componentName) {
       // The fucntion OnToggleShowWindow is supposed to be called here
    };
}

Only outside the ShowDialog function is bound at rendering the page. 仅在ShowDialog函数之外绑定渲染页面。 After what the showdialog is just a function that updates on the already bounded observables. 之后showdialog只是一个更新已经有界的observables的函数。

In my react component I have subscribed to the function, which I now try to retrieve: 在我的react组件中,我订阅了该函数,我现在尝试检索它:

this.props.toggleShowWindow.observable.subscribe(this.onToggleShowWindow); this.props.toggleShowWindow.observable.subscribe(this.onToggleShowWindow);

Put () after it instead of passing it to click . 在它之后放()而不是传递给它click

 $('#showWindowButton').click(self.OnToggleShowWindow (self.ShowWindow)); 

becomes: 变为:

self.OnToggleShowWindow(self.ShowWindow)()

Don't forget the () after it. 不要忘记它之后的() Pay close attention to the above line of code and make sure you don't miss any () out. 密切注意上面的代码行,确保你不会错过任何()

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

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