TypeError: document.getElementById(..) 为空,但 id 是正确的,因为我也类似地调



我正在尝试执行一个函数,但此错误不断出现在控制台中

TypeError: document.getElementById(...( is null
fetch http://localhost:8082/app.js:28368

这是 HTML 代码

<form id="player2">
<div class="form-group">
<label>Player 2 - From address:</label>
<input type=" type" class="form-control" id="fromAddress2">
</div>
<input type="submit" value="player2" class="btn-primary" />

</form>

这是应用程序.js


function fetch(){

document.getElementById("player2").addEventListener("submit", function(e){
e.preventDefault();

var fromAddress2 = document.getElementById("#player2 #fromAddress2").value;
console.log(fromAddress2);
OraclizeContract.deployed().then(function(instance) {
console.log("Initializing");
instance.deposit({from: fromAddress2, 
gas: 3000000,
value: web3.toWei(1, 'ether')}) //betAmount is a input box and fetching its value into betamount variable and passing it over here
.then(function(v){
console.log(v);
console.log("Function Executed");
});
}).then(function() {
console.log("Testing");
}).catch(function(e) {
console.log(e);
});
})
}

我已经检查了ID,但它们是正确的,无法弄清楚为什么这不起作用 任何帮助都非常感谢

var fromAddress2 = document.getElementById("#player2 #fromAddress2").value;

应该改为:

var fromAddress2 = document.querySelector("#player2 #fromAddress2").value;

相关内容

最新更新