JavaScript计算器:加号替代 [英] javascript calculator: plus sign alternatives

查看:62
本文介绍了JavaScript计算器:加号替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我研究当前问题时,文章似乎很有希望,但我我不能自己弄清楚这是否是我问题的答案。因此,如果有人可以帮助我,那将是非常棒的。

As I was researching my current question this article seemed to be promising but I could not figure out for myself if it was the answer to my question. So if anyone could help my that would be terrific.

这是我的功能:

function CalculateIMSUB(form) {
  var Atext = form.input_1.value;
  var Btext = form.input_2.value;
  var val = form.val.value;
  var A = eval(Atext);
  var B = eval(Btext);
  if (isNaN(A)) A = 0;
  if (isNaN(B)) B = 0;
  var answer = A - B;
  form.Answer.value = answer;
}

这是我的html:

<form>
  <INPUT TYPE=TEXT name="input_1" SIZE=15>
  <INPUT TYPE=TEXT name="input_2" SIZE=10>
  <INPUT TYPE="button" VALUE="+" name="SubtractButton"
  onclick="CalculateIMSUB(this.form)">
  <INPUT TYPE=TEXT NAME="Answer" SIZE=12>
  <input type="hidden" name="val" value="" />
</form>

我的问题:

我可以添加与 /

例如,当前,如果您要在输入1文本字段中键入10/5,然后单击计算,那么您在答案中将得到2的答案。文本域。因为大家都知道10除以5等于2,所以我希望能够在输入1文本字段中键入10/5并收到15的答案,这相当于10 + 5。非常感谢您的帮助,谢谢,这是我的 jsFiddle

For instance currently, if you were to type 10 / 5 in the input 1 text field and click calculate you would have an answer of 2 in the Answer text field. Since as we all know 10 divided by 5 equals 2. I would like to be able to type 10/5 into the input 1 text field and receive an answer of 15 which would be the equivalent to 10+5. Your help is greatly appreciated, thank you and here is my jsFiddle.

推荐答案

您也可以执行以下操作:

You could also do this:

var Atext = form.first.value.replace(/\//g,"+");
var Btext = form.second.value.replace(/\//g,"+");

这将替换所有幕后操作员,以便您的评估员将其作为加法处理。 (所以您可以放5/5/5并得到15,或者您拥有什么。)

Which would replace all division operators behind the scenes so that your eval will process them as addition. (So you could put 5/5/5 and get 15, or what have you.)

这篇关于JavaScript计算器:加号替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆