我有這個網站:的document.getElementById在外部HTML元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Notas</title>
<script type="text/javascript" src="notas.js"></script>
</head>
<body>
<p>Nombre alumno: </p>
<input type="text" name="nombre" id="nombre">
<p>Nota alumno:</p>
<input type="text" name="nota">
<p><br></p>
<input type="button" name="boton" value="Enviar" onclick="respuesta()">
</body>
</html>
而且此Javascript在另一個文件:
function respuesta(){
nombrej=document.getElementById("nombre");
document.write(nombrej);
}
的問題是,當我點擊按鈕的消息「null」 我在論壇上看到類似問題的解決方案,但由於有一個按鈕,我不知道該怎麼做。幫幫我!
這個JS是在另一個文件中?你有一個彈出窗口或一個iframe? – Teemu
適用於我:https://jsfiddle.net/1qa3vu1x/仔細檢查您是否包含正確的JS文件。 –
我看不出任何可能給出結果的方式,除了HTML輸入元素的序列化之外...這是我[測試代碼]時得到的結果(http://jsbin.com/zicafe/1/edit ?HTML,輸出)。在創建[MCVE]時,您似乎錯過了「確保該示例實際上重現了問題」步驟。 – Quentin