简体   繁体   English

如何在 onclick 事件上激活“if”条件

[英]How can I activate "if" condition on onclick event

I seem not to be able to figure out why my "if" part of the function is not working on onclick event.我似乎无法弄清楚为什么我的“if”部分功能对 onclick 事件不起作用。 I am trying to make a function to execute on onclick in HTML, it executes it but I always get "undefined" as a result...我正在尝试制作一个在 HTML 中的 onclick 上执行的函数,它执行它但我总是得到“未定义”的结果......

My HTML is:我的 HTML 是:

<button type="button" id="n1" class="btn btn-secondary btn-decor" onclick="pressNumber()">1</button>
                <button type="button" id="n2" class="btn btn-secondary btn-decor" onclick="pressNumber()">2</button>

and Javascript和 Javascript


var displayedValue = document.getElementById("calc-disp").textContent;

var fullNumber;

function pressNumber () {
    if (document.getElementById("n1")) {
        var fullNumber = displayedValue + numbers[1];
    }
    else if (document.getElementById("n2")) {
        var fullNumber = displayedValue + numbers[2];
    }
    displayValueNow();
}

// display result

function displayValueNow () {
    document.getElementById("calc-disp").innerHTML = fullNumber;
}```

thanks for any advice

Instead of using an if statement simply pass in the button number as an argument to the function and use that in your calculation.而不是使用 if 语句,只需将按钮编号作为参数传递给函数并在计算中使用它。

 const numbers = [0, 1, 2]; // Cache only the element const displayedValue = document.getElementById('calc-disp'); function displayValueNow(fullNumber) { displayedValue.textContent = fullNumber; } function pressNumber(n) { // Make sure you coerce your displayed value (a string) to // an integer before trying to add it to the number in the array var fullNumber = Number(displayedValue.textContent) + numbers[n]; // Pass in `fullNumber` as an argument to `displayValueNow` // so you're not using a global variable displayValueNow(fullNumber); }
 <button type="button" onclick="pressNumber(1)">1</button> <button type="button" onclick="pressNumber(2)">2</button> <div id="calc-disp"></div>

You were close, the if condition didn't work because your statement checked IF the element exists, not whether it was clicked on.你很接近,if 条件不起作用,因为你的语句检查了元素是否存在,而不是它是否被点击。 To do that, pass a param to your pressNumber function from your html (here I pass the button, you could pass the id directly or the value of the number).为此,请从您的 html 中将一个参数传递给您的 pressNumber 函数(这里我传递了按钮,您可以直接传递 id 或数字的值)。

 // declare your numbers array var numbers = [1, 2] // +so that it's a number not a string (try removing it) var fullNumber = +document.getElementById("calc-disp").textContent; // removed, we should only operate on the fullNumber variable // var displayedValue = document.getElementById("calc-disp").textContent; function pressNumber(button) { // in an if, document.getElementById("n1") just checks if the element exists - since it always exists, the else branch will never be executed if (button.id === "n1") { // remove the var so it changes the outer fullNumber fullNumber = fullNumber + numbers[0]; } else if (button.id === "n2") { fullNumber = fullNumber + numbers[1]; } displayValueNow(); } // display result function displayValueNow() { document.getElementById("calc-disp").innerHTML = fullNumber; }
 <button type="button" id="n1" class="btn btn-secondary btn-decor" onclick="pressNumber(this)">1</button> <button type="button" id="n2" class="btn btn-secondary btn-decor" onclick="pressNumber(this)">2</button> <div id="calc-disp">0</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM