猿问

如何为输入创建全局变量以及为什么要添加

function additon() {

  const value_1 = document.getElementById('value_1').value;

  const value_2 = document.getElementById('value_2').value;

  const answer = document.getElementById('answer');

  

  var additon = (value_1 + value_2);

  

  answer.innerHTML = additon;

}

function subtraction(){

  const value_1 = document.getElementById('value_1').value;

  const value_2 = document.getElementById('value_2').value;

  const answer = document.getElementById('answer');


  var subtraction = (value_1 - value_2);


  answer.innerHTML = subtraction;

}

Enter value 1 <input type="tel" id="value_1">

<br>

Enter value 2 <input type="tel" id="value_2">

<br>


Answer: <span class="answer" id="answer"></span>  

<br>

<button class="btn btn-success btn-lg" onclick="additon()">+</button>

<button class="btn btn-danger btn-lg" onclick="subtraction()">-</button>

<button class="btn btn-succes btn-lg" onclick="multiplication()">X</button>

<button class="btn btn-succes btn-lg" onclick="division()">/</button>


为什么要加1 + 1 = 11。减法工作正确以及如何为值输入创建全局变量我想成为一个实时更新的计算器。


尚方宝剑之说
浏览 104回答 3
3回答

陪伴而非守候

function additon() {&nbsp; &nbsp; const value_1 = document.getElementById('value_1').value;&nbsp; &nbsp; const value_2 = document.getElementById('value_2').value;&nbsp; &nbsp; const answer = document.getElementById('answer');&nbsp; &nbsp; var additon = (value_1*1 + value_2*1); // Multiplying changes the type to NUM&nbsp; &nbsp; answer.innerHTML = additon;}function subtraction(){&nbsp; &nbsp; const value_1 = document.getElementById('value_1').value;&nbsp; &nbsp; const value_2 = document.getElementById('value_2').value;&nbsp; &nbsp; const answer = document.getElementById('answer');&nbsp; &nbsp; var subtraction = (value_1*1 - value_2*1); // Multiplying changes the type to NUM&nbsp; &nbsp; answer.innerHTML = subtraction;}

MM们

它把它当作string而不是integer。试试这个:function additon() {&nbsp; var value_1 = document.getElementById('value_1').value;&nbsp; var value_2 = document.getElementById('value_2').value;&nbsp; var answer = document.getElementById('answer');&nbsp;&nbsp;&nbsp; var additon = +value_1 + +value_2;&nbsp;&nbsp;&nbsp; answer.innerHTML = additon;}Enter value 1 <input type="tel" id="value_1"><br>Enter value 2 <input type="tel" id="value_2"><br>Answer: <span class="answer" id="answer"></span>&nbsp;&nbsp;<br><button class="btn btn-success btn-lg" onclick="additon()">+</button>或者您可以使用Number()来处理数字。function additon() {&nbsp; var value_1 = Number(document.getElementById('value_1').value);&nbsp; var value_2 = Number(document.getElementById('value_2').value);&nbsp; var answer = document.getElementById('answer');&nbsp;&nbsp;&nbsp; var addition = value_1 + value_2;&nbsp;&nbsp;&nbsp;&nbsp; answer.innerHTML = addition;}Enter value 1 <input type="tel" id="value_1"><br>Enter value 2 <input type="tel" id="value_2"><br>Answer: <span class="answer" id="answer"></span>&nbsp;&nbsp;<br><button class="btn btn-success btn-lg" onclick="additon()">+</button>

肥皂起泡泡

我的答案你好,来自互联网的随机陌生人看起来你忘记将输入解析为数字所以这是你的固定代码function additon() {&nbsp; const value_1 = Number(document.getElementById('value_1').value);&nbsp; const value_2 = Number(document.getElementById('value_2').value);&nbsp; const answer = document.getElementById('answer');&nbsp; const additon = value_1 + value_2;&nbsp; answer.innerHTML = additon;}function subtraction() {&nbsp; const value_1 = Number(document.getElementById('value_1').value);&nbsp; const value_2 = Number(document.getElementById('value_2').value);&nbsp; const answer = document.getElementById('answer');&nbsp; const subtraction = value_1 - value_2;&nbsp; answer.innerHTML = subtraction;}// oh and i added the multiplication and division toofunction multiplication() {&nbsp; const value_1 = Number(document.getElementById('value_1').value);&nbsp; const value_2 = Number(document.getElementById('value_2').value);&nbsp; const answer = document.getElementById('answer');&nbsp; const multiplication = value_1 * value_2;&nbsp; answer.innerHTML = multiplication;}function division() {&nbsp; const value_1 = Number(document.getElementById('value_1').value);&nbsp; const value_2 = Number(document.getElementById('value_2').value);&nbsp; const answer = document.getElementById('answer');&nbsp; const division = value_1 / value_2;&nbsp; answer.innerHTML = division;}Enter value 1 <input type="tel" id="value_1" /><br />Enter value 2 <input type="tel" id="value_2" /><br />Answer: <span class="answer" id="answer"></span><br /><button class="btn btn-success btn-lg" onclick="additon()">+</button><button class="btn btn-danger btn-lg" onclick="subtraction()">-</button><button class="btn btn-succes btn-lg" onclick="multiplication()">X</button><button class="btn btn-succes btn-lg" onclick="division()">/</button>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答