简体   繁体   English

无法显示警报框JavaScript

[英]Can't show an alert box JavaScript

I'm trying to make this alert appear but it doesn't seems to work, I've tried almost everything but nothing actually works. 我正在尝试使此警报出现,但它似乎不起作用,我已经尝试了几乎所有方法,但实际上没有任何作用。

I've tried taking out the variable of the alert and it works, but I need it to show the result of the multiplication. 我试过取出警报的变量,它可以工作,但是我需要它来显示乘法的结果。

Then I've tried just adding the variable in the alert box but It doesn't work, and I can't find where is the problem. 然后,我尝试仅在警报框中添加变量,但是它不起作用,而且我找不到问题出在哪里。

<!DOCTYPE html> 
<html>
<head>    
<title>Multiplication</title>
</head>
<body>
  <h2> Ange 2 tal för att multiplicera</h2>
<table  border="0">
  <tr>
    <th>Första Tal</th>
    <th>Andra Tal</th>

  </tr>
  <tr>
    <td><input id="box1" type="text" oninput="calculate()" /></td>
    <td><input id="box2" type="text" oninput="calculate()" /></td>
    <td><input type="button" onclick="disp_alert()" value="Multiplicera" /></td>

  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

  <script>


function calculate() {
    var myBox1 = document.getElementById('box1').value; 
    var myBox2 = document.getElementById('box2').value;
    var result = document.getElementById('result'); 
    var myResult = myBox1 * myBox2;
    result.value = myResult;    
}
    function disp_alert() {
        alert("Resultaten blir"+result.value);
    }

  </script>  

</body>
</html>

The variable result only exists within the calculate() function. 变量result仅存在于calculate()函数中。 You need to retrieve the value again in the disp_alert() function. 您需要在disp_alert()函数中再次检索该值。

function disp_alert() {
    var result = document.getElementById('result'); 
    alert("Resultaten blir"+result.value);
}

Try this on your button : 在您的按钮上尝试以下操作:

<td><input type="button" onclick="disp_alert(calculate())" value="Multiplicera" /></td>

Your result variable is in the scope of your function calculate() so disp_alert() does not know what result is. 你的result变量是你的函数的范围calculate()这样disp_alert()不知道是什么result是。

var global_result; <---- this variable is accessible to all functions.

function calculate(){
 var result; <--- this is a local variable only accessible within this function
}

disp_alert(calculate()) <--- calculate innards become available to disp_alert

By putting the call off calculate() inside of disp_alert() that value becomes accessible. 通过将取消呼叫calculate()disp_alert()该值变为可访问。

You only need to calculate when you click the button. 您只需在单击按钮时进行计算。

 function calculate() { var myBox1 = document.getElementById('box1').value; var myBox2 = document.getElementById('box2').value; return myBox1 * myBox2; } function disp_alert() { var product = calculate(); alert(product); } 
 <body> <h2> Ange 2 tal för att multiplicera</h2> <table border="0"> <tr> <th>Första Tal</th> <th>Andra Tal</th> </tr> <tr> <td><input id="box1" type="text" /></td> <td><input id="box2" type="text" /></td> <td><input type="button" onclick="disp_alert()" value="Multiplicera" /></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table> </body> 

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

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