TypeError:document.getElementById(…)为null,但id是正确的,因为我也类似地调用了另一种形式

问题描述 投票:0回答:1

我正在尝试执行功能,但此错误不断出现在控制台中

TypeError:document.getElementById(...)为null获取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>

这里是App.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,但它们是正确的,无法弄清为什么它不起作用非常感谢您的帮助

javascript html web3 truffle metamask
1个回答
1
投票
var fromAddress2 = document.getElementById("#player2 #fromAddress2").value;
应改为:

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

© www.soinside.com 2019 - 2024. All rights reserved.