[英]How to pass variable from an HTML file to a JavaScript file
我有兩個不同的文件:a.html 和 a.js。 在 html 文件中,我有一個按鈕,當我單擊它時,我會調用位於 .js 文件中的 function。 此時,當我單擊按鈕時,我想將從 html 文件中收到的參數傳遞給 js 文件。 我已經嘗試從 js 中執行 Document.getElementById("idDelInput") 但由於輸入不在同一個文件中,因此它無法識別 Document.getElementById("idDelInput")。
function login() { //check the variable email }
<input id="email" type="text"> <button onClick="login()">Login</button>
您需要先將元素抓取到 Javascript 中,然后從中獲取值。 document.getElementById("email")
或document.querySelector("#email")
會將您的元素放入 javascript 文件中。 然后使用value
屬性來獲取它的值。
function login() { document.querySelector("#email").value; }
<input id="email" type="text"> <button onClick="login()">Login</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.