这个项目是要改一个人的名字,我有两个表格的两个名字。我永远不能改变第二个表格中的名字。 我被要求来这里,而不是在ethereum部分发帖当我在solidity中运行代码时,我可以改变名字,没有问题。当我用ganache运行它时,没有骰子,所以我认为这是一个javascript问题。
solidity
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 "需要改成其他东西。我也是一个javascript新手。我可以不在同一个页面上有两个输入吗? 我看了很多论坛都有类似的问题,但是我试过的都没有用。 谢谢你的帮助
EDIT
我把它改写如下,就可以用了
<!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>
我让每个 "表单部分 "的名称都是唯一的(唯一的头、表单名称、id......),然后引用这些名称。