jQuery的减少十进制;如何从html页面运行多个输入 [英] jquery reduce decimal; how to run for multiple input from html page

查看:112
本文介绍了jQuery的减少十进制;如何从html页面运行多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作中,我需要减少小数.以下JavaScript可以很好地减少小数,即将两个小数减少为一,

At my work, I need to reduce decimal. The following JavaScript does pretty well to reduce decimal, i.e., two decimal to one,

 var mynum = 32.34;
  myNumber = mynum.toFixed(2); // result = 32.3

我想同时从html格式转换几个值,例如

I want to convert several values in the same time from a html form, e.g.,

 82.77, 69.30, 43.75, 33.44, 26.88, 26.83, 24.89, 24.88, 24.74, 23.07, 19.31, 17.51.

82.8, 69.3, 44.0, 33.4, 26.8 ......

我希望有一个HTML表单,该表单接受输入数字,并在单击按钮时自动在同一HTML页面中检索结果.

I want to have an HTML form that takes input numbers and upon button click or automatically retrieves the result in the same html page.

推荐答案

JS

$('#formID').submit(function(){
    $('.decimalMe').val().toFixed(2);
});

(示例)HTML

<form id="formID">
    <input type="text" class="decimalMe" />
</form>

因此,给您要在'decimalMe'类上使用的所有输入字段(其他类名称可用).提交表单后(您可以触发此操作在另一个事件上发生,jQuery将在具有给定类名的所有输入上运行"toFixed" ...

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

小提琴: http://jsfiddle.net/moonspace/TA37P/1/

HTML

<input type="text" class="decimalMe" name="input1" />
<input type="text" class="decimalMe" name="input2" />

<a id="clickMe">Click me</a>

jQuery

$('#clickMe').click(function(){
    $('.decimalMe').each(function(){
        $(this).val( ($(this).val() * 1).toFixed(2) );
    });
});

基本上,这个jQuery: -遍历'decimalMe'的所有实例 -读取值($(this).val() -将其乘以1就可以得到一个数字(可能有更好的方法...) -'toFixed()'是否更改 -将更改后的数字弹出到来自'$(this).val(...)

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( . . . )

我之所以使用按钮和'click'事件,是因为我永远无法使表单提交在jsFiddle中正常工作. .

I've used a button and 'click' event simply because I can never get a form submit to work properly in jsFiddle . . .

这篇关于jQuery的减少十进制;如何从html页面运行多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆