Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将单词转换为数字,然后添加_Javascript_Html - Fatal编程技术网

Javascript 如何将单词转换为数字,然后添加

Javascript 如何将单词转换为数字,然后添加,javascript,html,Javascript,Html,我正在制作一个使用计算器的系统,但是,在任何给定的时间,用户必须点击一个单词,这个单词需要有一个值,例如20。请参见数学公式如何工作的示例: car = 20; 2 + (car + 1); 这个公式的结果是23 这些词语和价值观​​来自数据库 有人知道如何用javascript实现这一点吗 javascript代码: function addChar(input, character) { if (input.value == null || input.value == "0"){

我正在制作一个使用计算器的系统,但是,在任何给定的时间,用户必须点击一个单词,这个单词需要有一个值,例如20。请参见数学公式如何工作的示例:

car = 20;
2 + (car + 1);
这个公式的结果是23

这些词语和价值观​​来自数据库

有人知道如何用javascript实现这一点吗

javascript代码:

function addChar(input, character) {
    if (input.value == null || input.value == "0"){
        input.value = character;
    }
    else{
        input.value += character;
    }
}
function cos(form) {
    form.display.value = Math.cos(form.display.value);
}
function sin(form) {
    form.display.value = Math.sin(form.display.value);
}
function tan(form) {
    form.display.value = Math.tan(form.display.value);
}
function sqrt(form) {
    form.display.value = Math.sqrt(form.display.value);
}
function ln(form) {
    form.display.value = Math.log(form.display.value);
}
function exp(form) {
    form.display.value = Math.exp(form.display.value);
}
function sqrt(form) {
    form.display.value = Math.sqrt(form.display.value);
}
function deleteChar(input) {
    input.value = input.value.substring(0, input.value.length - 1)
}
function changeSign(input) {
    if (input.value.substring(0, 1) == "-"){
        input.value = input.value.substring(1, input.value.length);
    }
    else{
        input.value = "-" + input.value
    }
}
function compute(form) {
    form.display.value = eval(form.display.value)
}
function square(form) {
    form.display.value = eval(form.display.value) *
            eval(form.display.value)
}

使用前缀和后缀忽略部分匹配词替换错误。
我使用了%

它与PHP相关吗?或者只是javascript?你的计算器是用什么语言制作的?HTML?使用对象
dict={'car':20}
then
dict['car']+1
is
21
php和javascript使用javascript进行计算,设置单词和值​​来自数据库。那么您的问题是什么?从数据库检索数据?答案很好,但确实需要使用
%
?请检查我的,并告诉我你的意见。但我如何在变量数据中添加字符串和值?使用前缀和后缀忽略部分匹配词替换错误。我用了%。如果您使用不带%的值,那么当您替换car的值时,如果您有两个变量,如“car”和“carvalue”。carvalue也得到了替换。比如20value(carvalue)。所以我加了%
var data = {"car":20};
var formula = " 2 + (%car% + 1)";
for (var index in data){
    formula = formula.replace(new RegExp("%"+index+"%","g"),data[index]);
}
alert(eval(formula));