Javascript Math Functions

How can I use these JavaScript math functions ?

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

Can you give a small example? Thank.

+3
source share
7 answers

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

Click here to test a working example. (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 you click any link, it finds all the inputs textin myformand iterates over them.

Inside each function thisrefers to the current element input.

+2
source

JQuery , Javascript, Javascript JQuery, .

:

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

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

: ...

<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));
+7

JavaScript - , jQuery, -, JavaScript. jQuery, JavaScript.

, jQuery :

// Set each single-line textbox 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)
    }
});
+1

, jQuery reduce().

, , , , .

- map, jQuery . , . . [1,2,3] -> (map x2) -> [2,4,6].

, . reduce ( 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);

, :

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

jquery map

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

0

Looking at the original question that was posted, he clearly states that he calculates the square of all values ​​in the form without submitting the form.

I think a keyboard would be a better solution.

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

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

See http://informativejavascript.blogspot.nl/ for more details.

0
source

I was also looking for a solution, and I saw a lot of questions here that don't work (even this one), if someone asks a question 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 id switches are entered

0
source

Source: https://habr.com/ru/post/1785948/


All Articles