为了账号安全,请及时绑定邮箱和手机立即绑定

将文本字段输入除以设置的数字,显示结果

将文本字段输入除以设置的数字,显示结果

PHP
慕哥6287543 2023-11-06 15:35:19
我有一个文本字段,需要除以某些输入。我尝试使用下面的代码,但遇到了问题。它不考虑小数点,如果使用小数点,将返回 0。<input id="number" type="text"> ÷ 3.181818<br />Your output is: <div id="output"></div><script type="text/javascript">//obtain reference to number html elementvar number = document.getElementById('number');//obtain reference to output html elementvar output = document.getElementById('output');//what you want to divide tovar division = '3.181818'; // can't be 0, or the world will explode//add an eventlistener to the change event on the number elementnumber.addEventListener('keyup', function(e) {  //get the value from the input  var val = parseFloat(number.value);  //check if it is a nan  if(!isNaN(val)) {     //round it to 0 digits     output.innerHTML = (val / division).toFixed(0) + "";  } else {     output.innerHTML = 'that\'s not a number!';  }});</script>因此,例如,如果有人在文本框中输入他们的敏感度为1.2,那么就会这样做1.2 / 3.181818 = 0.377142878690.37714287869成为他们新的敏感性。
查看完整描述

1 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

删除toFixed并设置div为浮动3.181818:


//obtain reference to number html element

var number = document.getElementById('number');


//obtain reference to output html element

var output = document.getElementById('output');


//what you want to divide to

var division = 3.181818; // can't be 0, or the world will explode


//add an eventlistener to the change event on the number element

number.addEventListener('keyup', function(e) {


  //get the value from the input

  var val = parseFloat(number.value);


  //check if it is a nan

  if (!isNaN(val)) {


    output.innerHTML = val / division;

  } else {

    output.innerHTML = 'that\'s not a number!';

  }

});

<input id="number" type="text"> ÷ 3.181818<br /> Your output is:

<div id="output"></div>


查看完整回答
反对 回复 2023-11-06
  • 1 回答
  • 0 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信