使用JavaScript从HTML按钮获取值

时间:2016-01-14 19:58:55

标签: javascript html function calculator

我正在制作一个计算器,我已经用html和css创建了计算器,但是我试图通过在显示屏上点击注册按钮来向前推进,这就是我现在的问题。我是一个相当陌生的JavaScript,所以如果有人能指出我正确的方向,如何做到这一点或在哪里找到答案我会很感激。

这是我正在处理的部分,试图让按钮'7'注册,以便我可以做其他人。

<div class="container-fluid calc" >
 <div class="display">
  <label type="text" id="screen">0</label>

   <div class="buttons">

     <button onClick='calculate()' id='myButton'>7</button>
     <button>8</button>
     <button>9</button> 

这是我放在一起的JS

function calculate(){
  var num = document.getElementById('#myButton').contentValue;
  document.getElementById('screen').innerHTML = num;
}

calculate();

4 个答案:

答案 0 :(得分:4)

您应该使用.contentValue函数而不是#函数执行此操作,此外,您不应该使用document.getElementById中的function calculate(){ var num = document.getElementById('myButton').innerHTML; document.getElementById('screen').innerHTML = num; } calculate(); 在jQuery中,所以只需要ID就足够了

{{1}}

希望这有帮助!

答案 1 :(得分:4)

您需要从

更新
 var num = document.getElementById('#myButton').contentValue;

 var num = document.getElementById('myButton').innerHTML;

答案 2 :(得分:0)

更新

function calculate(){
  var num = document.getElementById('#myButton').contentValue;
  document.getElementById('screen').innerHTML = num;
}

function calculate(){
  var num = document.getElementById('myButton').innerText;
  document.getElementById('screen').innerText = num;
}

答案 3 :(得分:0)

另一种选择是你可以这样的数据属性:

#include <iostream>
#include <string>
#include <functional>

class AbstractSolver {
  public:
  virtual ~AbstractSolver(){}
  virtual double solve() = 0;
  virtual void setSystem(std::function<double(double,double)> system) = 0;
};

class ConcreteSolver : public AbstractSolver {
  protected:
    double stepSize;
    double initialValue;
    std::function<double(double, double)> system;
  public:
  ConcreteSolver(double stepSize,
                 double initialValue) :
     stepSize(stepSize),
     initialValue(initialValue) {}

  double solve() {
    // implementation here ...
  }

  void setSystem(std::function<double(double,double)> system) {
    this->system = system;
  }
};

class Model {
  protected:
    std::function<double(double,double,double)> system;
    AbstractSolver * solver;
  public:
    Model(AbstractSolver * solver,
          std::function<double(double, double, double)> system ) {
      this->solver = solver;
      this->system = system;
    }

    ~Model() {
       delete solver;
    }

    double getSolution(double tau) {
      std::function<double(double, double)> sys =
          [this, tau](double t, double y) { return system(t, y, tau); };
      solver->setSystem(sys); // SIGSEGV
      return solver->solve();
    }
};

int main(){
  std::function<double(double, double, double)> system = 
    [](double t, double y, double tau) {
         return 0;// return some mathematical expression
      };
  AbstractSolver * solver = new ConcreteSolver(0, 1);
  Model model = Model(solver, system);
  model.getSolution(0.1);

}

并且这样:

<button onClick='calculate()' id='myButton' data-value="7">7</button>