简体   繁体   English

Javascript初学者-谁能告诉我为什么我的按钮不起作用?

[英]Javascript beginner - Can anyone tell me why my button doesn't work?

jsfiddle is here --> http://jsfiddle.net/diabetesjones/015k1tjn/5/ jsfiddle在这里-> http://jsfiddle.net/diabetesjones/015k1tjn/5/

here's an example of one that does work: http://jsfiddle.net/Ahopefulmachine/dkhj8o38/ 这是一个有效的示例: http : //jsfiddle.net/Ahopefulmachine/dkhj8o38/

Regarding the second jsfiddle (not mine), I don't quite get why his works without using document.getElementById on the button. 关于第二个jsfiddle(不是我的),我不太明白为什么他的工作方式不使用按钮上的document.getElementById。

i feel like my problem is in the click function itself of : 我觉得我的问题出在点击功能本身上:

document.getElementById('mainButton').onclick = function () {

thanks :) 谢谢 :)

working now: http://jsfiddle.net/doniyor/015k1tjn/9/ 现在正在工作: http//jsfiddle.net/doniyor/015k1tjn/9/

you had document.getElementByID which should be document.getElementById 您有document.getElementByID应该是document.getElementById

AND

  1. you didnot convert strings to int like numberOne = parseInt(numberOne, 10); 您没有像numberOne = parseInt(numberOne, 10);那样将字符串转换为int numberOne = parseInt(numberOne, 10); where 10 is radix for decimal. 其中10是小数的基数。
  2. you had question == ADD which should be question == 'ADD' 您有question == ADD应该是question == 'ADD'

There are three things wrong with your code: 您的代码存在三件事:

  1. It's getElementById not getElementByID (case-sensitive). getElementById而不是getElementByID (区分大小写)。
  2. You didn't convert the strings you get from the input to numbers. 您没有将从输入中获得的字符串转换为数字。 You can do this easily by adding a + in front of the document.getElementById('number1').value; 您可以通过在document.getElementById('number1').value;前面添加一个+来轻松实现此目的document.getElementById('number1').value;
  3. You were doing comparisons against variables, not strings. 您正在对变量而不是字符串进行比较。 Ex question == ADD instead of question == 'ADD' question == ADD而不是question == 'ADD'

See corrected jsFiddle example 请参阅更正的jsFiddle示例

document.getElementById('mainButton').onclick = function () {

    //Getting values of inputs and saving them to variables
    var numberOne = +document.getElementById('number1').value;
    var numberTwo = +document.getElementById('number2').value;

    //Setting values of the equations
    var addition = (numberOne + numberTwo);
    var subtraction = (numberOne - numberTwo);
    var multiplication = (numberOne * numberTwo);
    var division = (numberOne / numberTwo);

    //Prompting user for their desired equation
    var question = prompt("Do you wish to ADD, SUBTRACT, MULTIPLY, or DIVIDE?").toUpperCase();

    //If statement to show the proper equation based on the user's prior prompt
    if (question == 'ADD') {
        alert('I added the shit out of those numbers for you - turns out it is ' + addition);
    } else if (question == 'SUBTRACT') {
        alert('Did some of this, some of that, some minusing - your answer is ' + subtraction);
    } else if (question == 'MULTIPLY') {
        alert('Yeah, I multipled the numbers, big whoop, wanna fight abouddit? the answers over there --> ' + multiplication);
    } else if (question == 'DIVIDE') {
        alert('This ones my favorite, I love a good division - ' + division);
    };

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我为什么我的导航不起作用? - Can anyone please tell me why my navigation doesn´t work? 谁能告诉我为什么我的代码不能正常工作? 游戏石头、纸、剪刀。 Java 脚本 - Can anyone tell me why my code doesn't work properly? Game ROCK, PAPER, SCISSORS. Java Script 有人告诉我为什么这个Jquery在IE中不起作用吗? - Anyone tell me why this Jquery doesn't work in IE? 谁能告诉我这个 MD Bootstrap Snippet 不起作用? - Can anyone tell me this MD Bootstrap Snippet doesn't work? 谁能告诉我为什么我的JavaScript点击无法正常工作? - can anyone tell me why my javascript clicks isnt working? 这个提交按钮是用javascript创建的,不起作用,有人可以帮我吗? - this submit button is created with javascript and doesn't work, can anyone help me? 我的代码在$(document).ready()函数中不起作用。 谁能帮我理解原因? - My code doesn't work within a $(document).ready() function. Can anyone help me understand why? 有人可以告诉我为什么这不起作用吗? (javascript html 节点选择器) - Can someone tell me why this doesn't work? (javascript html node selector) 谁能告诉我为什么我的最后2个如果其他语句不起作用? 全新的JavaScript在这里 - Can anyone tell me why my last 2 if else statements are not working? Brand new to JavaScript here 谁能告诉我为什么这个javascript函数调用不起作用? - Can anyone tell me why this javascript function call isn't working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM