问题描述
我正在用JavaScript和HTML编写计算器程序.我有一个带有值"="的按钮.在那种情况下,当用户单击计算器中的"=" 按钮时.它应该开始评估他/她到目前为止提供的输入.但是当用户单击"="按钮时,我试图像这样
I am doing a calculator program in javascript and HTML. I have a button with a value '=' in it. In that one when a user click on "=" button in calculator. it should start evaluate the input he/she had given so far. But when a user click '=' button i tried to compare like this
$(".calcinput").click(function () {
var res = $(this).val();
if (res == '=') {
//code to handle when '=' is pressed;
}
}
但是它没有用,请帮助我.
but it didn't work please help me.
推荐答案
您的代码good
足以处理事件..只需在comapre中添加一个额外的=
Your code is good
enough to handle the event .. Just add an extra =
in comapre
$(".calcinput").click(function () {
var res = $(this).val();
//if (res == '=') {
if (res === '=') {
//code to handle when '=' is pressed;
}
});
下一步检查使用div
或button
或其他元素表示button =
Next check where you used div
or button
or something else element to represent the button =
如果是button
,请使用val()
提取值
var res = $(this).val();
如果元素是div
,则可能是innerHTML
或innerTEXT
If the element is div
then it could be innerHTML
or innerTEXT
var res = $(this).innerHTML;
//Or
var res = $(this).innerText;
您可以添加一条额外的行,以检查您是否获得了正确的element
You can add an extra line to check whether you getting proper value of element
var res = $(this).val();
alert(res);
这篇关于如何在JavaScript中比较等于'='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!