简体   繁体   English

如何使两个按钮在Javascript中工作?

[英]how to make two buttons work in Javascript?

I am trying to learn javascript and I am making a simple Fahrenheit Celsius application. 我正在尝试学习JavaScript,并且正在制作一个简单的华氏摄氏温度应用程序。 The problem that I currently have is only one of two buttons work. 我目前遇到的问题只是两个按钮之一起作用。 in case there are 2 if statements , only button Fahrenheit2Celcius button works, in case there is an if... else if statement then only button Celcius2Fahrenheit works, but they never work both. 如果有2条if语句,则只有按钮Fahrenheit2Celcius按钮起作用,如果有if ... else if语句,则只有按钮Celcius2Fahrenheit起作用,但它们两者都不起作用。 Would you please explain what I am doing wrong? 您能解释我做错了吗? Here is my code: 这是我的代码:

 function C2F() { var Stoinost = document.getElementById('Stoinost').value; var Fahrenheit = (Stoinost * 1.8) + 32; var fahrenheit2 = Math.round(Fahrenheit*100)/100; var C2F_but = document.getElementById("C2F"); var Celcius = (Stoinost - 32) * (5/9); var Celcius2 = Math.round(Celcius*100)/100; var F2C_but = document.getElementById("F2C"); if (C2F_but){ document.getElementById("result").innerHTML = fahrenheit2; } if (F2C_but){ document.getElementById("result").innerHTML = Celcius2; } } 
 <table id="myTable"> <tr> <tr><td><p> Input Value: <input type="number" id="Stoinost" size="20" ></p></td></tr> <td> <button onclick="C2F()" id="F2C">Fahrenheit2Celcius</button> <button onclick="C2F()" id="C2F">Celcius2Fahrenheit</button> </td> <tr><td><p> <div id="result">Result is displayed here.</div> </p></td></tr> </tr> </table> 

You can use this when calling a function in an onclick handler and detect which element is clicked by checking for attributes on it like the id: 您可以在onclick处理程序中调用函数时使用this函数,并通过检查其上的属性(例如ID)来检测单击了哪个元素:

 function C2F(btn) { console.log(btn.id); } 
 <button onclick="C2F(this)" id="F2C">Fahrenheit2Celcius</button> <button onclick="C2F(this)" id="C2F">Celcius2Fahrenheit</button> 

But a much simpler and possible better solution would be to just use two functions. 但是,一个更简单,可能更好的解决方案是仅使用两个函数。

You're using the wrong condition in your if statement. 您在if语句中使用了错误的条件。 When you check if (c2f_but), it will always return true, because the button exists. 当您检查(c2f_but)是否存在时,它将始终返回true,因为该按钮存在。 You're not actually asking something about it. 您实际上并没有对此提出任何疑问。

What you want to know is if the button clicked is c2f_button. 您想知道的是单击的按钮是否为c2f_button。 In that case, you would have to pass the click event as a parameter, and check if its target matches the button, or at least check some property of that button. 在这种情况下,您将必须将click事件作为参数传递,并检查其目标是否与按钮匹配,或者至少检查该按钮的某些属性。

However, I don't think you need that kind of checking in this case. 但是,在这种情况下,我认为您不需要这种检查。 You can simply pass the calculation you want to make as a parameter of your function, like in the example below: 您可以简单地将要进行的计算作为函数的参数传递,如以下示例所示:

 function C2F(option) { var Stoinost = document.getElementById('Stoinost').value; var Fahrenheit = (Stoinost * 1.8) + 32; var fahrenheit2 = Math.round(Fahrenheit*100)/100; /*var C2F_but = document.getElementById("C2F");*/ var Celcius = (Stoinost - 32) * (5/9); var Celcius2 = Math.round(Celcius*100)/100; /*var F2C_but = document.getElementById("F2C");*/ if (option == 'c2f'){ document.getElementById("result").innerHTML = fahrenheit2; } if (option == 'f2c'){ document.getElementById("result").innerHTML = Celcius2; } } 
 <table id="myTable"> <tr> <tr><td><p> Input Value: <input type="number" id="Stoinost" size="20" ></p></td></tr> <td> <button onclick="C2F('f2c')" id="F2C">Fahrenheit2Celcius</button> <button onclick="C2F('c2f')" id="C2F">Celcius2Fahrenheit</button> </td> <tr><td><p> <div id="result">Result is displayed here.</div> </p></td></tr> </tr> 

Why don't you make two functions for different functionalities? 为什么不为不同的功能创建两个功能?

 function C2F() { var Stoinost = document.getElementById('Stoinost').value; var Fahrenheit = (Stoinost * 1.8) + 32; var fahrenheit2 = Math.round(Fahrenheit*100)/100; document.getElementById("result").innerHTML = fahrenheit2; } function F2C() { var Stoinost = document.getElementById('Stoinost').value; var Celcius = (Stoinost - 32) * (5/9); var Celcius2 = Math.round(Celcius*100)/100; document.getElementById("result").innerHTML = Celcius2; } 
 <table id="myTable"> <tr> <tr><td><p> Input Value: <input type="number" id="Stoinost" size="20" ></p></td></tr> <td> <button onclick="F2C()" id="F2C">Fahrenheit2Celcius</button> <button onclick="C2F()" id="C2F">Celcius2Fahrenheit</button> </td> <tr><td><p> <div id="result">Result is displayed here.</div> </p></td></tr> </tr> </table> 

I would also suggest you to check your HTML, you have a <tr> directly inside another <tr> . 我也建议你检查你的HTML,你有一个<tr>直接在另一<tr> It should be only <td> 's inside <tr> 's. 它应该是唯一<td>的内部<tr>的。

the problem you are having is that C2F_but is a reference to the DOMNode, which is always truthy. 您遇到的问题是C2F_but是对DOMNode的引用,这始终是事实。 So both conditionals will always evaluate to true . 因此,两个条件都将始终为true To do what you are wanting to do you need to get the Event object that is being submitted by the click event and then check to see which of those two DOMNodes was the triggering element. 要执行您想做的事情,您需要获取click事件正在提交的Event对象,然后检查一下这两个DOMNodes中的哪一个是触发元素。

 function C2F(event) { var Stoinost = document.getElementById('Stoinost').value; var Fahrenheit = (Stoinost * 1.8) + 32; var fahrenheit2 = Math.round(Fahrenheit*100)/100; var C2F_but = document.getElementById("C2F"); var Celcius = (Stoinost - 32) * (5/9); var Celcius2 = Math.round(Celcius*100)/100; var F2C_but = document.getElementById("F2C"); if (event.target == C2F_but){ document.getElementById("result").innerHTML = fahrenheit2; } if (event.target == F2C_but){ document.getElementById("result").innerHTML = Celcius2; } } 
 <table id="myTable"> <tr> <td><p> Input Value: <input type="number" id="Stoinost" size="20" ></p></td> <td> <button onclick="C2F(event)" id="F2C">Fahrenheit2Celcius</button> <button onclick="C2F(event)" id="C2F">Celcius2Fahrenheit</button> </td> </tr> <tr><td><p> <div id="result">Result is displayed here.</div> </p></td></tr> </table> 

You are over writing results. 您正在写结果。 Instead, set a parameter in your function to know which button was press 而是在函数中设置参数以知道按下了哪个按钮

 function C2F(type) { var Stoinost = document.getElementById('Stoinost').value; var Fahrenheit = (Stoinost * 1.8) + 32; var fahrenheit2 = Math.round(Fahrenheit*100)/100; var C2F_but = document.getElementById("C2F"); var Celcius = (Stoinost - 32) * (5/9); var Celcius2 = Math.round(Celcius*100)/100; var F2C_but = document.getElementById("F2C"); if (type == 'cf'){ document.getElementById("result").innerHTML = fahrenheit2; }else{ document.getElementById("result").innerHTML = Celcius2; } } 
 <table id="myTable"> <tr> <tr><td><p> Input Value: <input type="number" id="Stoinost" size="20" ></p></td></tr> <td> <button onclick="C2F('fc')" id="F2C">Fahrenheit2Celcius</button> <button onclick="C2F('cf')" id="C2F">Celcius2Fahrenheit</button> </td> <tr><td><p> <div id="result">Result is displayed here.</div> </p></td></tr> </tr> 

You need to tell the function the action (button) you want to perform. 您需要告诉该功能要执行的操作(按钮)。

In this case you can pass the action as a parameter when calling the function C2F 在这种情况下,可以在调用函数C2F时将操作作为参数传递

<button onclick="C2F('f2c');" id="F2C">Fahrenheit2Celcius</button>
<button onclick="C2F('c2f');" id="C2F">Celcius2Fahrenheit</button>

And catching that action in the function: 并在函数中捕获该动作:

if (action == 'c2f'){
    document.getElementById("result").innerHTML = fahrenheit2;
}

if (action == 'f2c'){
    document.getElementById("result").innerHTML = Celcius2;
}

I've created a working example here: Demo 我在这里创建了一个工作示例: 演示

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

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