Jquery Reduce Decimal; How To Run For Multiple Input From Html Page
Solution 1:
you can create an array of your values and loop your code and write it in an other array:
var number_list = [ 82.77, 69.30, 43.75, 33.44, 26.88, 26.83, 24.89, 24.88, 24.74, 23.07, 19.31, 17.51];
var result = [];
for(var k in number_list){
result.push(number_list[k].toFixed(1));
}
// if u not use IE for dev uncomment the line below for see result in console//console.log(result);
e little example with form with input field jq
$('#myid').submit(function(e){
e.preventDefault();
var number_list = eval("["+ $(".numberlist").val() +"]");
var result = [];
for(var k in number_list){
// add new number to result arrayif(typeof number_list[k] == "number") result.push(number_list[k].toFixed(1));
//append new number to a result list: 1.55 -> 1.6
$("#result").append("<li>"+number_list[k]+" -> "+number_list[k].toFixed(1)+"</li>");
}
});
html
<form id="myid">
<input class="numberlist"type="text"/>
</form>
<ul id="result"></ul>
inside this input write number separated by ",".
like: 1.56,213.32423424,5.234234
Solution 2:
JS
$('#formID').submit(function(){
$('.decimalMe').val().toFixed(2);
});
(sample) HTML
<form id="formID">
<input type="text" class="decimalMe" />
</form>
So, give all the input fields you want to work on a class of 'decimalMe' (other class names are available). When the form is submitted (and you can trigger this to happen on another event, the jQuery will run 'toFixed' on all inputs with the given class name...
Updated working version
fiddle: http://jsfiddle.net/moonspace/TA37P/1/
HTML
<input type="text"class="decimalMe" name="input1" />
<inputtype="text"class="decimalMe"name="input2" /><aid="clickMe">Click me</a>
jQuery
$('#clickMe').click(function(){
$('.decimalMe').each(function(){
$(this).val( ($(this).val() * 1).toFixed(2) );
});
});
Basically, this jQuery: - loops through all the instantiations of 'decimalMe' - reads the value ($(this).val() - multiplies it my 1 to make it a number (there are probably better ways of doing this...) - does the 'toFixed()' change - pops the changed number back into the field it came from '$(this).val( . . . )
I've used a button and 'click' event simply because I can never get a form submit to work properly in jsFiddle . . .
Solution 3:
var x = [ 82.77, 69.30, 43.75, 33.44, 26.88, 26.83, 24.89, 24.88, 24.74, 23.07, 19.31, 17.51], y =[];
for(var i = 0; i < x.length; ++i) {
y.push(x[i].toFixed(2));
}
console.log(y);
Solution 4:
Here is a fiddle that does it on input fields with a target class in a form
//when user clicks submit button
$('#submitform').click(function(){
$(this).closest('form').find('.toFixed1').each(function(){
var val = parseFloat($(this).val());
//only perform operation on an actual numberif(!isNaN(val)){
//overwrite the value of the field with the reduced decimal number
$(this).val(val.toFixed(1));
}
})
})
Post a Comment for "Jquery Reduce Decimal; How To Run For Multiple Input From Html Page"