Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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_Jquery_Html_Switch Statement_Trigonometry - Fatal编程技术网

在度模式下获取JavaScript计算器

在度模式下获取JavaScript计算器,javascript,jquery,html,switch-statement,trigonometry,Javascript,Jquery,Html,Switch Statement,Trigonometry,我正在通过将输入(以度为单位)转换为弧度(因为Math.cos()函数只接受弧度),让JavaScript计算器在度模式下执行计算。我使用一个按钮根据此代码在每个模式之间切换: $('#button-mode').click(function() { var type = $(this).val(), $div = $('.mode'); switch (type) { case 'DEG': $div.ht

我正在通过将输入(以度为单位)转换为弧度(因为
Math.cos()
函数只接受弧度),让JavaScript计算器在度模式下执行计算。我使用一个按钮根据此代码在每个模式之间切换:

$('#button-mode').click(function() {

  var type = $(this).val(),
            $div = $('.mode');

        switch (type) {
        case 'DEG':
            $div.html('DEG');
            $('#button-mode').html('Rad');
            $('#button-mode').val('RAD');
            break;
        case 'RAD':
            $div.html('RAD');
            $('#button-mode').html('Deg');
            $('#button-mode').val('DEG');
            break;
                }
            });
现在,这仅仅改变了位于输入显示中的div的HTML,以便用户知道计算器处于什么模式。我尝试使用带有此按钮模式的计数器,并让余弦按钮根据模式按钮的点击次数执行不同的功能(
cos
cosDeg
-如下)

function cos(form) {
  form.display.value = Math.cos(form.display.value);
}

function cosDeg(form) {
  form.display.value = Math.cos(form.display.value*(Math.PI/180));
}
当出现这种情况时,
cosDeg
函数似乎不会产生接近正确值的值。例如,如果用户输入180并点击余弦按钮,则返回值类似于-0.59846。。。当它应该返回-1时

我也尝试过用这个片段来达到预期的效果,但它根本不起作用,我也不知道确切的原因是什么:

$('#button-cos').click(function(){
    var argument = this.form.display.value;

if ($("#button-mode").html() == 'Rad') {
    var cosArg = Math.cos(argument);
setTimeout (function(){
    $("#disp").val('cosArg');
    },0.5);
}

else if ($("#button-mode").html() == 'Deg') {
    var argumentDEG = argument * (Math.PI/180);
    var cosArgDEG = Math.cos(argumentDEG );
setTimeout (function(){
    $("#disp").val('cosArgDEG');
    },0.5);
}
}))


显然,我需要帮助。如何获得一个按钮来控制模式并从
cosDeg
函数中获得正确的返回值?

对于初学者,您的代码中存在语法错误
Math.cos((form.display.value*(Math.PI/180));
的左括号太多。@JosephMarikle更改了它,但仍然不能正常工作。