简体   繁体   English

如何制作将其值转换为十分之一的 HTML 数字输入(可能使用 jQuery)

[英]How to make an HTML number input that transforms its value to tenths (possibly using jQuery)

Let's say I have an input field like假设我有一个输入字段,例如

<input type='number' id='input-tenths' />

What I want to achieve in this, is the digits I put into this field being treated as "tenths" instead of whole numbers, for example:我想在此实现的是,我在此字段中输入的数字被视为“十分之一”而不是整数,例如:

  • When I input a 3 in this input field, I want the value of the field to be 0.3当我在此输入字段中输入3时,我希望该字段的值为0.3
  • When, after that, I input a 5 , I want the value to become 3.5之后,当我输入5 ,我希望该值变为3.5
  • ... then a 9 , the value should become 35.9 ...然后是9 ,该值35.9

I have so far tried to add a hidden field <input type='hidden' id='hidden-tenths'/> , and a function like到目前为止,我已经尝试添加一个隐藏字段<input type='hidden' id='hidden-tenths'/> ,以及一个像

$('#input-tenths').on('input', function(e){
    $('#hidden-tenths').val($('#hidden-tenths').val() + '' + $(this).val().slice(-1));
    $(this).val($('#hidden-tenths').val() / 10);
    $('#hidden-tenths').val($(this).val() * 10);
});

but this doesn't always work as expected (for example, when I backspace in the original input, a digit is appended rather than removed, as can be expected from using the .slice(-1) .但这并不总是按预期工作(例如,当我在原始输入中退格时,会附加而不是删除数字,正如使用.slice(-1)所预期的.slice(-1)

What would be a good way to achieve the expected behaviour, where I'm trying to stay away from explicitly binding all key presses and thereby essentially reprogramming the input field (except when this is the only way to achieve this).什么是实现预期行为的好方法,我试图避免显式绑定所有按键,从而基本上重新编程输入字段(除非这是实现此目的的唯一方法)。

 $('#input-tenths').on('input', function(e) { $('#hidden-tenths').val($('#hidden-tenths').val() + '' + $(this).val().slice(-1)); $(this).val($('#hidden-tenths').val() / 10); $('#hidden-tenths').val($(this).val() * 10); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type='number' id='input-tenths' /> <input type='number' id='hidden-tenths' />

Change opacity from .3 to 0 when happy.开心时将不透明度从 0.3 更改为 0。 You may want to suppress decimals你可能想抑制小数

 $('#hidden-tenths').on('input', function(e) { $('#input-tenths').val((this.value/10).toFixed(1)) });
 #input-tenths { opacity:1; position:absolute; z-index:0; } #hidden-tenths { opacity:.3; position:absolute; z-index:100; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type='number' id='input-tenths' /> <input type='number' id='hidden-tenths' />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML使用选择表单更改输入选择(可能使用JQuery吗?) - HTML Using select form to change input selection (Possibly using JQuery?) 如何使用 jquery 使子 tr 输入复选框值在 html 表中为真? - How to make child tr input checkbox value true in html table using jquery? HTML5:使用键盘输入,类型为“数字”的输入字段仍将接受比其“最大值”更高的值 - HTML5 : Input field with the type of 'number' will still accept higher value than its 'max' using keyboard inputs 如何覆盖HTML5输入数字的步进功能? 使用jQuery - How to override HTML5 input number step function? Using jQuery 如何使用jQuery从输入数字中读取值? - How can I read value from input number by using jQuery? 如何通过其值jquery增加一个数字 - How to increment a number by its value jquery 如何使用 Jquery 或 javascript 使 A 中的表单输入值出现在 B 中 - How to make form input value in A to appear in B using Jquery or javascript 如何使用 jQuery 或 Javascript 制作模态弹出窗口以获取输入值? - How to make a Modal popup to take an input value using jQuery or Javascript? HTML和Javascript按钮可以使文本区域中的文本变为粗体和粗体,可能使用JQuery吗? - HTML and Javascript button to make text in a textarea bold and unbold, Possibly using JQuery? 访问网站时如何使视频大小增加。 HTML java脚本可能是JQuery吗? 视频动画 - how to make video size increases when site is visited. HTML java script possibly JQuery? Video animation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM