简体   繁体   English

点击前点击事件触发器

[英]Click event triggers before click

I have a function that opens dialog windows. 我有一个打开对话框窗口的功能。 When the user clicks on the sign up button the sign up dialog should open. 当用户单击“注册”按钮时,应会打开“注册”对话框。 However, the dialog opens as soon as the page loads. 但是,该对话框将在页面加载后立即打开。 If I use an anonymous function to handle the event it works fine, but I want to reuse the openDialog function for other dialogs (login, etc) so I don't want it to be anonymous. 如果我使用匿名函数来处理事件,则它可以正常工作,但我想将openDialog函数用于其他对话框(登录等),因此我不希望它是匿名的。

var ready;
ready = function () {
    $('.js-join-button').on('click', openDialog(event, signUp));

    function openDialog(event, dialogType) {
        event.preventDefault ? event.preventDefault() : event.returnValue = false;
        dialogType.dialog('open');
    }
...
}
$(document).ready(ready);

You need to wrap your function in an anonymous function. 您需要将函数包装在匿名函数中。 Your code will run through and simply trigger the function otherwise. 您的代码将一直运行,否则将简单触发该函数。

$('.js-join-button').on('click', function() {
    openDialog(event, signUp)
});

Now, for example, if your function did not take any parameters, you could do 现在,例如,如果您的函数没有任何参数,则可以执行

 $('.js-join-button').on('click', openDialog);

This passes a reference to the function. 这会将对函数的引用传递给该函数。 With the () it triggers the function. 使用()触发函数。 Without, it's a function reference. 没有它,它是一个函数参考。

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

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