Web3js - 按下按钮并触发功能

时间:2018-02-03 20:38:31

标签: javascript jquery web3js

我在我的前端使用web3js library连接我的智能合约后端

见下面我的代码:

App = {
  web3Provider: null,
  contracts: {},

  init: function () {
    return App.initWeb3();
  },

  initWeb3: function () {

    // Is there an injected web3 instance?
    if (typeof web3 !== 'undefined') {
      App.web3Provider = web3.currentProvider;
    } else {
      // If no injected web3 instance is detected, fall back to Ganache
      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
    }
    web3 = new Web3(App.web3Provider);

    return App.initContract();
  },

  initContract: function () {

    $.getJSON('CryptoSportsToken.json', function (data) {
      // Get the necessary contract artifact file and instantiate it with truffle-contract
      var CryptoSportsTokenArtifact = data;
      App.contracts.CryptoSportsToken = TruffleContract(CryptoSportsTokenArtifact);

      // Set the provider for our contract
      App.contracts.CryptoSportsToken.setProvider(App.web3Provider);

    });

    return App.createPerson();
  },
  /*
    bindEvents: function () {
      var owner = $('#owner').val();
      var name = $('#name').val();
      var price = $('#price').val();

      console.log("App: \n");      
      console.log(App);
      console.log("App.contracts: \n");      
      console.log(App.contracts);

      console.log(owner + " " + name + " " + price)

      //createPromoPerson(address _owner, string _name, uint256 _price)
      $(document).on('click', '.btn-create', App.createPromoPerson(owner, name, price));
    },
  */
  createPerson: function () {

    console.log("lolonator")
    var cryptosportInstance;

    //##########
    $(document).on('click', '.btn-create', function () {
      var owner = $('#owner').val();
      var name = $('#name').val();
      var price = $('#price').val();

      console.log(owner + " " + name + " " + price)

      App.contracts.CryptoSportsToken.deployed().then(function (instance) {
        cryptosportInstance = instance;

        console.log(cryptosportInstance)

        return cryptosportInstance.createPromoPerson(owner, name, price).call();
      }).catch(function (err) {
        console.log(err.message);
      })
    });
  },
};

$(function () {
  $(window).load(function () {
    App.init();
  });
});

在我的前端,我有一个按钮,可以触发创建一个新人:

<div class="container">
    <h1>Sports Person</h1>
    <label for="owner" class="col-lg-2 control-label">Owner Address: </label>
    <input id="owner" type="text" />
    <br/>

    <label for="name" class="col-lg-2 control-label">Name: </label>
    <input id="name" type="text" />
    <br/>

    <label for="price" class="col-lg-2 control-label">Price: </label>
    <input id="price" type="text" />
    <br/>

    <button class="btn btn-default .btn-create" type="button" data-id="0">Create</button>
  </div>

当我按下按钮时,我基本上没有结果。没有console.log被触发。

从chrome下面看我的console.log:

enter image description here

有什么建议吗?

感谢您的回复!

1 个答案:

答案 0 :(得分:1)

您的HTML中的某个“创建”按钮类中存在拼写错误。而不是:

<button class="btn btn-default .btn-create" type="button" data-id="0">

应该是:

<button class="btn btn-default btn-create" type="button" data-id="0">

这是因为.btn-create中的$(document).on('click', '.btn-create', function () {实际上是一个CSS类选择器。