Javascript按钮无响应

时间:2019-09-19 08:10:01

标签: javascript firebase-authentication

是Java语言的新手,并且正在遵循本教程here

哪个将android应用程序连接到fireabse Web应用程序。 但是在单击时,firebase Web应用程序中的登录按钮没有响应。 它会产生以下错误

Uncaught ReferenceError: config is not defined at app.js:11 (anonymous) @ app.js:11 errors.ts:101 Uncaught FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app). at l (http://localhost:5000/__/firebase/6.6.1/firebase-app.js:1:10041) at Object.i [as auth] (http://localhost:5000/__/firebase/6.6.1/firebase-app.js:1:9592) at http://localhost:5000/:41:22

我尝试使用从Why is Javascript button not working?获得的onclick函数,但仍然没有响应。

我还尝试使用firebase init,然后使用firebase serve代替http-server,但这还是行不通的

<body class="bg-dark">
  <script src="/__/firebase/6.6.1/firebase-app.js"></script>

          <script src="/__/firebase/6.6.1/firebase-auth.js"></script>

    <div id="login-card" class="card">
        <div class="card-body">
            <h1>Admin Panel</h1>
            <form id="login-form">
                <div class="form-group">
                    <label for="email">email</label>
                    <input type="email" id="email" class="form-control" />
                </div>
                <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" id="password" class="form-control" />
                </div>
                <div class="form-group">
                    <button id="btn-login" type="button" class="btn btn-primary">Login</button>
                </div>
            </form>
        </div>
    </div>

    <script src="js/app.js"></script>

    <script>

        firebase.auth().onAuthStateChanged(function(user){
            if(user){
                window.location.href = "admin.html";
            }
        });

    </script>

</body>

app.js

var firebaseConfig = {
...
};

firebase.initializeApp(config);

firebase.auth.Auth.Persistence.LOCAL; 

$("#btn-login").click(function(){

    var email = $("#email").val();
    var password = $("#password").val(); 

    var result = firebase.auth().signInWithEmailAndPassword(email, password);

    result.catch(function(error){
        var errorCode = error.code; 
        var errorMessage = error.message; 

        console.log(errorCode);
        console.log(errorMessage);
    });

});

1 个答案:

答案 0 :(得分:2)

您需要使用$(document).ready()包装以使其起作用:

$(document).ready(function() {
  $("#btn-login").click(function() {
    console.log("It's working");
  });
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<button id="btn-login" type="button" class="btn btn-primary">Login</button>


OR

您需要使用选择器将document上的事件绑定

$(document).on('click', '#btn-login', function() {
  console.log("It's working");
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<button id="btn-login" type="button" class="btn btn-primary">Login</button>