Javascript 数学函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4715408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:37:53  来源:igfitidea点击:

Javascript Math Functions

javascriptjquerymath

提问by Eray

How can I use these JavaScript math functions?

如何使用这些 JavaScript数学函数

For example, I want to compute the square of all <input>values in a form, without submiting the form.

例如,我想计算<input>表单中所有值的平方,而不提交表单。

Can you give a little example? Thank you.

你能举一个小例子吗?谢谢你。

采纳答案by user113716

You can act on each individual input using an each()(docs)loop.

您可以使用each()(文档)循环对每个单独的输入进行操作。

Click here to test a working example.(jsFiddle)

单击此处测试工作示例。(jsFiddle)

$('a.square').click(function() {
    $('#myform :text').each(function() {
        this.value *= this.value;
    });
});

$('a.square_root').click(function() {
    $('#myform :text').each(function() {
        this.value = Math.sqrt(this.value);
    });
});

When either link is clicked, it finds all the textinputs in myformand iterates over them.

单击任一链接时,它会查找所有text输入myform并对其进行迭代。

Inside the each function, thisrefers to the current inputelement.

在 each 函数内部,this指的是当前input元素。

回答by Tom Gullen

JQuery doesn't need to support math functions as it is an addon library for Javascript, you can still use Javascript in your JQuery code, so you can still use all the native math functions.

JQuery 不需要支持数学函数,因为它是 Javascript 的插件库,您仍然可以在 JQuery 代码中使用 Javascript,因此您仍然可以使用所有本机数学函数。

Examples:

例子:

Addition

添加

var x = 1;
var y = 2;
var lol = x+y;
alert(lol);

Subtraction

减法

var x = 10;
var y = 1;
var lol = x-y;
alert(lol);

Edit: Now we understand your question a little better...

编辑:现在我们更好地理解了你的问题......

<input type="text" id="field1" value="16" />
<input type="text" id="field2" value="25" />
<input type="text" id="field3" value="36" />

var field1Value = document.getElementById("field1").value;
var field2Value = document.getElementById("field2").value;
var field3Value = document.getElementById("field3").value;

alert(Math.sqrt(field1Value ));
alert(Math.PI * field2Value);
alert(Math.sin(field3Value));

回答by PleaseStand

JavaScriptis the programming language, not jQuery, which is a library for web application programming written in JavaScript. To effectively use jQuery, you need to know JavaScript.

JavaScript是编程语言,而不是 jQuery,后者是用 JavaScript 编写的 Web 应用程序编程库。要有效地使用 jQuery,您需要了解 JavaScript。

It is, however, possible to use jQuery's functionality to easily work with multiple textboxes at once:

但是,可以使用 jQuery 的功能轻松地同时处理多个文本框:

// Set each single-line textbox's value to the square
// of its numeric value, if its value is in fact a number.
$('input:text').each(function() {
    var num = +this.value;
    if(!isNaN(num)) {
        this.value = num * num;    // or Math.pow(num, 2)
    }
});

回答by Orbling

It would be quite useful if jQuery had a reduce()function.

如果 jQuery 有一个reduce()函数,那将非常有用。

When dealing with lists of data, most functional languages, and indeed most traditional languages these days, have methods that perform a repetitive function over the entire list, taking each element in turn and applying a function to it.

在处理数据列表时,大多数函数式语言,实际上是当今大多数传统语言,都有对整个列表执行重复函数的方法,依次获取每个元素并对其应用函数。

The simplest of these is map, which jQuery implements for you. This takes a list and applies a function to each element and returns the list of results, one result per entry in the list. eg. [1,2,3] -> (map x2) -> [2,4,6].

其中最简单的是map,jQuery 为您实现。这需要一个列表并将一个函数应用于每个元素并返回结果列表,列表中的每个条目一个结果。例如。[1,2,3] -> (map x2) -> [2,4,6].

Sometimes you want a total or collective result from a list, rather than a list of individual mappings. This is where the reduce(or fold) operation comes in. Unfortunately jQuery does not have this method available as standard, so below is a plugin for it. A reducefunction takes an accumulator value and the value of the current element, and returns the modified accumulator, which will be passed on to the next call. eg. [1,2,3,4] -> (reduce + [initial:0]) -> 10 = ( ( ( (0 + 1) + 2 ) + 3 ) + 4 )or ([1,2,3,4] -> (reduce * [initial:1]) -> 24 = ( ( ( (1 * 1) * 2 ) * 3 ) * 4 ).

有时,您需要一个列表中的总体或集体结果,而不是单个映射的列表。这就是reduce(or fold) 操作的用武之地。不幸的是 jQuery 没有这种方法作为标准可用,所以下面是它的插件。一个reduce函数接受一个累加器值和当前元素的值,并返回修改后的累加器,该累加器将被传递到下一次调用。例如。[1,2,3,4] -> (reduce + [initial:0]) -> 10 = ( ( ( (0 + 1) + 2 ) + 3 ) + 4 )([1,2,3,4] -> (reduce * [initial:1]) -> 24 = ( ( ( (1 * 1) * 2 ) * 3 ) * 4 )

(function($) {
    $.reduce = function(arr, callback, initial) {
        var accumulator = initial || 0;

        $.each(arr, function(index, value) {
            accumulator = callback(accumulator, value, index);
        });

        return accumulator;
    }
})(jQuery);

Then you can use it like this to get a sum of squares:

然后你可以像这样使用它来获得平方和:

var answer = $.reduce($('input:text'), function(acc, elem) {
    var cVal = $(elem).val();
    return acc + cVal * cVal;
}, 0);

回答by Ouahib Abdallah

i was looking for a solution too , and i saw a lot of questions here that doesn't work (even this one) in case someone wondering like me , here is my working solutiuon :

我也在寻找解决方案,我在这里看到很多问题(即使是这个),以防有人像我一样想知道,这是我的工作解决方案:

$("#apport").keyup( 
function(){
var apport = parseFloat($("#apport").val());
var montant = parseFloat($("#montant-financer").val());
var moinmontant = parseFloat(montant) - parseFloat(apport);     
$("#montant-financer").val(moinmontant);    
}
);

All the id's selector are input

所有id的选择器都是输入

回答by Josiah Ruddell

Use the jquery map functionto create an array

使用jquery map函数创建数组

$('input:text').map(function() {
  return this.value * this.value; // math calculation goes here
}).get();

See a live example

看一个活生生的例子

回答by kamal

Looking at the initial question that was posted, it clearly states compute the square of all values in a form, without submiting the form.

查看发布的最初问题,它清楚地说明计算表单中所有值的平方,而不提交表单。

i think keyup would be the best solution.

我认为 keyup 将是最好的解决方案。

$("input").keyup(function () {
  var value = $(this).val();
  var x=value*value;
  $("p").text(x);
}).keyup();

Click here to check the working example. http://jsfiddle.net/informativejavascript/Sfdsj/3/

单击此处查看工作示例。 http://jsfiddle.net/informativejavascript/Sfdsj/3/

For more details visit http://informativejavascript.blogspot.nl/

有关更多详细信息,请访问http://informativejavascript.blogspot.nl/