簡體   English   中英

HTML表單,getElementById無法在javascript函數內運行。 登錄頁面

[英]HTML Form, getElementById not working inside javascript function. Login Page

我在這里很困惑,我有HTML表單,我想從中將信息發送到javascript。 (這將調用offisde進行登錄)。 每次我輸入var username = document.getElementById("user").value; 在該函數內部,控制台日志上什么也沒有顯示。 但是,如果我將其取出,它的行為正常。

這是我的HTML

<form class="form-login">
            <h2 class="form-login-heading">sign in now</h2>
            <div class="login-wrap">
                <input type="text" class="form-control" placeholder="User ID" name="user" autofocus>
                <br>
                <input type="password" class="form-control" placeholder="Password" name="passwd" >
                <button class="btn btn-theme btn-block" Value="Submit" type="submit" onclick="Calculate()"><i class="fa fa-lock"></i> SIGN IN</button> 

這是我的JavaScript

<Script>
function Calculate(){

        console.log("In function, test 1");


        var username = document.getElementById("user").value; <-- if remove this, console/alert apper, but add this and no consoles.log appear. Not even the one above



        alert ("Login successfully");
            window.location.replace("index.html");
            //window.location = "index.html"; // Redirecting to other page.
            return false; 
</script>

因此,我對為什么不能在javascript函數中使用getElementById感到困惑。

`

沒有ID為user元素,您有一個名稱為user的輸入,向其中添加屬性id="name" ,然后就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM