简体   繁体   English

Javascript HTML按钮单击不起作用

[英]Javascript HTML button click not working

I've created a login system using HTML, JavaScript, Ajax and PHP. 我使用HTML,JavaScript,Ajax和PHP创建了一个登录系统。

I'm trying to implement the log out function now, here's what I have: 我正在尝试实现注销功能,这是我所拥有的:

First there is a div in my html, which says welcome to anyone on the site not logged in: 首先,我的html中有一个div,它表示欢迎您登录该网站上的任何人:

<div id="welcome" style="postion:absolute; top:0; float:right;">Welcome to SIK!!!!</div>

I then have a JavaScript function which is called when a user is logged in or registered to amend the div to say hi to the specific user, and show a log out button. 然后,我有了一个JavaScript函数,该函数将在用户登录或注册时进行修改,以修改div以向特定用户打个招呼,并显示注销按钮。

function show_logged_in(success)
{

    var is_logged_in = success;

    var dataString = {"reg":invalid, "login":invalid,"is_logged_in":is_logged_in};

    $.ajax({
        type:"POST",
        url:"PHP/class.ajax.php",
        data:dataString,
        dataType:'JSON',
        success: function(username) {
            alert("User is shown");
            user = username;
            $('#welcome').html('<p>Hi ' + user + '</p><p><button id="logout_but" 
   type="button">Log Out</button></p>');
        },
        error: function() {
            alert("ERROR in show_logged_in");
        }
    });
}

And then when the user clicks logout, in my main js file there is a function: 然后,当用户单击注销时,在我的主要js文件中有一个函数:

$("#logout_but").click(ui.logout);

here is my main js file altogether: 这是我的主要js文件:

$(document).ready(function(){


//----------Login/Register Gui --------------
$('#load-new').hide();
$('#reg').hide();
$('#login').hide();

//create new instance of gui class
var ui = new Gui();

//if login_but is clicked do ui.login function
$('#login_but').click(ui.login);
//if reg_but is clicked do ui.register function
$('#reg_but').click(ui.register);
//if login_sumbit button is clicked do ui.login_ajax function
$("#login_submit").click(ui.login_ajax);

$("#reg_submit").click(ui.register_ajax);

$("#logout_but").click(ui.logout);

});

So now you can see the order of the calls, and also the click function that is called for the logout button is: 因此,现在您可以看到调用的顺序,并且注销按钮所调用的click函数是:

this.logout = function() {

    alert("clicked");

    var dataString = {"is_logged_out":invalid};

    $.ajax({
        type:"POST",
        url:"PHP/class.logout.php",
        data:dataString,
        dataType:'JSON',
        success: function(success){
            alert("User is logged out");
            $('#welcome').html('<p>Welcome to SIK</p>');
        },
        error: function() {
            alert('ERROR in log out');
        }
    })

}

As you can see I have and alert at the top of the last function I posted to show me if the function is actually being called. 如您所见,我在发布的最后一个函数的顶部警告我,以告诉我该函数是否真正被调用。

My problem is when I click the logout button nothing happens. 我的问题是,当我单击注销按钮时,什么都没有发生。 I also don't see the alert, thus the problem is occurring on the click. 我也没有看到警报,因此点击发生问题。

Any help with this situation would be great. 在这种情况下的任何帮助将是巨大的。

That's because you create the logout button dynamically and AFTER the click handler is trying to attach itself to the element in your main js file. 这是因为您是动态创建注销按钮的,并且在单击处理程序尝试将其自身附加到主js文件中的元素之后。 As a result, the handler doesn't find the #logout_but element and therefore doesn't attach to anything. 结果,该处理程序找不到#logout_but元素,因此未附加任何内容。

To fix that, you could use a delegate instead of a click handler: 要解决此问题,您可以使用委托代替点击处理程序:

Replace this: 替换为:

$("#logout_but").click(ui.logout);

with

$("#welcome").on("click", "#logout_but", ui.logout);

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

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