這篇文章給大家分享的是有關javascript能不能獲取input的值的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
javascript可以獲取input值,方法:首先使用“document.getElementById("id值")”語句獲取到input元素對象;然后使用“input元素對象.value”語句獲取input元素的值。
本教程操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。
javascript可以獲取input值
代碼示例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>JavaScript中獲取input元素value值</title> <link rel="stylesheet" href=""> <script> function printInputValue() { var inputValue = document.getElementById("demo1").value; console.log(inputValue); document.getElementById("demo2").innerHTML = inputValue; } </script> </head> <body> <input type="text" id="demo1"> <button onclick="printInputValue()">獲取input元素value值</button> <p id="demo2"></p> </body> </html>
運行結果:
說明:
getElementById() 方法可返回對擁有指定 ID 的第一個對象的引用。
value 屬性可設置或返回文本域的值。
感謝各位的閱讀!關于“javascript能不能獲取input的值”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。