Solidity

來自第二種形式的輸入不會顯示

  • May 14, 2020

該項目是更改某人的名字,我有兩個名字的兩個表格。當我在solidity中執行程式碼時,我可以更改名稱,沒問題。當我用 ganache 執行它時,第二種形式的名稱更改永遠不會“佔用”。

堅固性

pragma solidity ^0.4.2;

contract Election {
   string public candidateName;
   string public candidateotherName;



   function Election () public {
       candidateName = "Candidate 1";
       candidateotherName = "Candidate2";
   }

   function setCandidate (string _name) public {

      candidateName = _name;
   }


    function setOtherCandidate (string _othername) public {
    candidateotherName = _othername;

}
}

html

<!DOCTYPE html>
<html lang="en">
 <body>
         <div id="content">
           <h4 id="candidateName"></h4>

           <form id="form">

               <div class="input-group">
                 <input  name="candidateName">
                 </input>

                   <button type="submit" >Add Candidate</button>

               </div>
             </div>
           </form>


           <div id="othercontent">
             <h4 id="candidateotherName"></h4>

             <form id="form1" >

                 <div class="input-group">
                   <input name="candidateotherName"  id='testid'>
                   </input>
                     <button type="submit" >Add other Candidate</button>
                   </div>
               </div>
             </form>


   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   <script src="js/bootstrap.min.js"></script>
   <script src="js/web3.min.js"></script>
   <script>

     if (typeof web3 !== 'undefined') {
       web3 = new Web3(web3.currentProvider);
     } else {
       web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
     }

         web3.eth.defaultAccount = web3.eth.accounts[0];


     var contractAbi = [I know i put in the ABI here, i'm just leaving it blank];


     var contractAddress = 'I know i have to put in the address here, I am leaving it blank';


     var contract = web3.eth.contract(contractAbi).at(contractAddress);


     contract.candidateName(function(err, candidateName) {
       $('#candidateName').html(candidateName);
     });


     contract.candidateotherName(function(err, candidateotherName) {
       $('#candidateotherName').html(candidateotherName);
     });


     $('form').on('submit', function(event) {
       event.preventDefault();
       contract.setCandidate($('input').val());
     })

     $('form1').on('submit', function(event) {
       event.preventDefault();
       contract.setOtherCandidate($('testid').val());
     })


   </script>
 </body>
</html>

我試過改變

contract.setOtherCandidate($('testid').val());

contract.setOtherCandidate($('input').val());

並更改輸入類型,更改所有變數的名稱,並嘗試

使用 # 引用 id 的東西。我無法在送出時更新第二個表單,我不知道為什麼,但仍然堅信這是由於底部附近的行需要將“testid”更改為其他內容。感謝您的任何幫助

無論我說什麼,表格1都會改變,但表格2不會

如果有人偶然發現這一點,這就是我為解決這個問題所做的。我為每個“表單部分”(唯一的標題、表單名稱、id …)設置了唯一的每個名稱,然後引用了這些名稱。一些 id 不是唯一的。我還使用 # 來引用 id。

<!DOCTYPE html>
<html lang="en">
 <body>

           <h4 id="candidateName"></h4>

           <form id="form">


                 <input  id="nameone" name="candidate1Name">
                 </input>

                   <button type="submit" >Add Candidate</button>



           </form>

           <h5 id="othercandidateName"></h5>

           <form id="otherform">


                 <input  id="othernameone" name="othercandidate1Name">
                 </input>

                   <button type="submit" >Add Candidate</button>



           </form>



   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   <script src="js/bootstrap.min.js"></script>
   <script src="js/web3.min.js"></script>
   <script>

     if (typeof web3 !== 'undefined') {
       web3 = new Web3(web3.currentProvider);
     } else {
       web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
     }

         web3.eth.defaultAccount = web3.eth.accounts[0];


     var contractAbi =[my way cool ABI]
;


     var contractAddress = 'my way cool address';


     var contract = web3.eth.contract(contractAbi).at(contractAddress);


     contract.candidateName(function(err, candidateName) {
       $('#candidateName').html(candidateName);
     });



     $('#form').on('submit', function(event) {
       event.preventDefault();
       contract.setCandidate($('#nameone').val());
     })

     contract.othercandidateName(function(err, othercandidateName) {
       $('#othercandidateName').html(othercandidateName);
     });


           $('#otherform').on('submit', function(event) {
             event.preventDefault();
             contract.othersetCandidate($('#othernameone').val());
           })

   </script>
 </body>
</html>

引用自:https://ethereum.stackexchange.com/questions/83368