更新时间:2023-10-29 23:09:34
这是您当前的代码:
var alpha = 0;
var bravo = 0;
var charlie = 0;
$ b $('#buttonplus')。click(function(){
var alpha = $(#output)。val();
});
$ b $('#buttonequals')。click(function(){
var bravo = $(#output)。val();
var charlie = alpha + bravo;
$('#output')。val($(charlie).val());
});
这些是全局变量:
var alpha = 0;
var bravo = 0;
var charlie = 0;
但是您正在中创建新的局部变量,请点击
$>处理程序:
$('#buttonplus')。click(function(){
var alpha = $(#output)。val();
});
移除 var
来存取全局变量:
$('#buttonplus')。click(function(){
alpha = $(#output ).val();
});
#output
to alpha: alpha = $(#output ).VAL();
如果 alpha
是2并且 bravo
是3,那么 alpha + bravo
会是 23
p>
您可以通过加一个加号将它强制为数字:
alpha = + $(#output)。val();
$('#output')。val($(charlie).val());
…这样做:
$('#output')。val(charlie);
更正后的代码:
var alpha = 0;
var bravo = 0;
var charlie = 0;
$('#buttonplus')。click(function(){
alpha = + $(#output)。val();
});
'('#buttonequals')。click(function(){
bravo = + $(#output)。val();
charlie = alpha + bravo ;
$('#output')。val(charlie);
});
i am having a little trouble here. im building a calculator and im working on the general idea on it and im confused on why my variable charlie will not appear in the output text box. All the variables are correct in the end ( i checked them with the alert function) but in the end it seems that it just wont show up! any help would be appreciated!
<!DOCTYPE html>
<body>
<div id="content">
<form name="calc">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script id="NumberButtons">
$(document).ready(function(){
$('#button1').click(function(){
$('#output').val($(this).val());
});
$('#button2').click(function(){
$('#output').val($(this).val());
});
$('#button3').click(function(){
$('#output').val($(this).val());
});
$('#button4').click(function(){
$('#output').val($(this).val());
});
$('#button5').click(function(){
$('#output').val($(this).val());
});
$('#button6').click(function(){
$('#output').val($(this).val());
});
$('#button7').click(function(){
$('#output').val($(this).val());
});
$('#button8').click(function(){
$('#output').val($(this).val());
});
$('#button9').click(function(){
$('#output').val($(this).val());
});
$('#button0').click(function(){
$('#output').val($(this).val());
});
$('#buttonclear').click(function(){
$('#output').val($(this).val());
});
});
</script>
<script id="MathSymbolButtons">
var alpha = 0;
var bravo = 0;
var charlie = 0;
$(document).ready(function(){
$('#buttonplus').click(function(){
var alpha = $("#output").val();
window.alert(alpha)
});
$('#buttonequals').click(function(){
var bravo = $("#output").val();
window.alert(bravo)
var charlie = alpha + bravo;
$('#output').val($(charlie).val());
});
});
</script>
<table>
<tr>
<td>
<input type="text" id="output">
<br>
</td>
</tr>
<tr>
<td>
<input type="button" name="one" value = "1" id="button1">
<input type = "button" name = "two" value = "2" id="button2" >
<input type = "button" name = "three" value = "3" id="button3">
<input type = "button" name = "four" value = "4"id="button4">
<input type = "button" name = "five" value = "5" id="button5">
<input type = "button" name = "six" value = "6" id="button6">
<input type = "button" name = "seven" value = "7" id="button7">
<input type = "button" name = "eight" value = "8" id="button8">
<input type = "button" name = "nine" value = "9" id="button9">
<input type = "button" name = "zero" value = "0" id="button0">
<input type = "button" name = "add" value = "+" id="buttonplus">
<input type = "button" name = "evaluate" value = " = " id="buttonequals">
<br>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Here is your current code:
var alpha = 0;
var bravo = 0;
var charlie = 0;
$('#buttonplus').click(function(){
var alpha = $("#output").val();
});
$('#buttonequals').click(function(){
var bravo = $("#output").val();
var charlie = alpha + bravo;
$('#output').val($(charlie).val());
});
These are global variables:
var alpha = 0;
var bravo = 0;
var charlie = 0;
But you're creating new local variables within the click
handlers:
$('#buttonplus').click(function(){
var alpha = $("#output").val();
});
Remove var
to access the global variables instead:
$('#buttonplus').click(function(){
alpha = $("#output").val();
});
#output
to alpha:alpha = $("#output").val();
If alpha
is 2 and bravo
is 3, then alpha + bravo
would be 23
.
You can coerce it to a number by prepending a plus sign:
alpha = +$("#output").val();
$('#output').val($(charlie).val());
… do this:
$('#output').val(charlie);
Corrected code:
var alpha = 0;
var bravo = 0;
var charlie = 0;
$('#buttonplus').click(function(){
alpha = +$("#output").val();
});
$('#buttonequals').click(function(){
bravo = +$("#output").val();
charlie = alpha + bravo;
$('#output').val(charlie);
});