Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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 jquery减少小数;如何从html页面运行多个输入_Javascript_Jquery_Html - Fatal编程技术网

Javascript jquery减少小数;如何从html页面运行多个输入

Javascript jquery减少小数;如何从html页面运行多个输入,javascript,jquery,html,Javascript,Jquery,Html,在我的工作中,我需要减少小数。下面的JavaScript很好地减少了十进制数,即从两个十进制数减少到一个 var mynum = 32.34; myNumber = mynum.toFixed(2); // result = 32.3 我想同时从html表单转换多个值,例如 82.77, 69.30, 43.75, 33.44, 26.88, 26.83, 24.89, 24.88, 24.74, 23.07, 19.31, 17.51. 到 我想要一个HTML表单,它接受输入的数字

在我的工作中,我需要减少小数。下面的JavaScript很好地减少了十进制数,即从两个十进制数减少到一个

 var mynum = 32.34;
  myNumber = mynum.toFixed(2); // result = 32.3
我想同时从html表单转换多个值,例如

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

我想要一个HTML表单,它接受输入的数字,点击按钮或自动检索同一HTML页面中的结果。

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=[];
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);
对于(变量i=0;i
JS

(示例)HTML

基本上,这个jQuery: -循环遍历“decimalMe”的所有实例化 -读取值($(this).val() -将它乘以我的1,使其成为一个数字(可能有更好的方法来实现这一点…) -“toFixed()”是否更改 -将更改后的数字弹出到它来自“$(this).val(…)的字段中


我之所以使用按钮和“单击”事件,只是因为我无法让表单提交在JSFIDLE中正常工作…

您可以创建一个值数组,循环代码并将其写入其他数组:

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);
带有输入字段的表单的小示例 jq

$('#myid')。提交(函数(e){
e、 预防默认值();
var number_list=eval(“[”+$(“.numberlist”).val()+“]”);
var结果=[];
用于(编号列表中的变量k){
//将新编号添加到结果数组
if(type of number_list[k]=“number”)result.push(number_list[k].toFixed(1));
//将新编号追加到结果列表:1.55->1.6
$(“#结果”)。追加(“
  • ”+数字列表[k]+“->”+数字列表[k]。固定(1)+“
  • ”; } });
    html

    
    
      在该输入内,写入编号以“,”分隔


      比如:1.56213.32423424,5.234234

      这里有一个小提琴,它在表单中有一个目标类的输入字段上演奏

      //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 number
              if(!isNaN(val)){
                  //overwrite the value of the field with the reduced decimal number
                  $(this).val(val.toFixed(1));
              }
      
          })
      })
      

      Java:一种在Java虚拟机中运行的编程语言。JavaScript:一种在web浏览器中运行的完全不同的脚本语言。jQuery:一种使使用DOM节点更简单的JavaScript库。您的问题只包含JavaScript,没有Java或jQuery。“以下jQuery做得很好…”这不是jQuery。这是本机JavaScript。它不会将小数点减少到一位。我尝试了它,但它不起作用,这是脚本;请看我修改后的答案,它现在应该起作用了……我遇到了错误;{“错误”:“请使用POST请求”}这是您确定包含jQuery的脚本(JSFIDLE窗口左上角)?我编辑了你的JSFIDLE:它是有效的,但是我如何才能以82.8、69.3、44.0、33.4、26.8的格式得到结果……我还提出了一个使用awk的新问题。这项工作做得很好,我如何发布html表单中的值?这可以完成这项工作,谢谢。那么我如何在值之间留出空间呢?
      <form id="formID">
          <input type="text" class="decimalMe" />
      </form>
      
      <input type="text" class="decimalMe" name="input1" />
      <input type="text" class="decimalMe" name="input2" />
      
      <a id="clickMe">Click me</a>
      
      $('#clickMe').click(function(){
          $('.decimalMe').each(function(){
              $(this).val( ($(this).val() * 1).toFixed(2) );
          });
      });
      
      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);
      
      $('#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 array
            if(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>");
          }
      });
      
      <form id="myid">
         <input class="numberlist" type="text"/>
      </form>
      <ul id="result"></ul>
      
      //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 number
              if(!isNaN(val)){
                  //overwrite the value of the field with the reduced decimal number
                  $(this).val(val.toFixed(1));
              }
      
          })
      })