Event return null value when user enter decimal on

2020-05-03 10:19发布

Edit working on Firefox, only Chrome bug (and maybe others)

I try to get all event from a simple <input type="number" step="any" /> :

$('input[type=number]').on('input', function() {
  $('#value').text($(this).val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="number" step="any"/>
<br/>
<strong>Value:</strong> <span id="value"></span>

All append good when I press, or update the number. But when I try to put a decimal point . the value is null.

I use jQuery here, but the problem is the same with Vanilla JS.

2条回答
地球回转人心会变
2楼-- · 2020-05-03 10:40

The closest I can get is:

$('input[type=number]').on('input', function() {
  $(this).select();

  var value = window.getSelection().toString();
  window.getSelection().empty();

  $('#value').text(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="number" step="any"/>
<br/>
<strong>Value:</strong> <span id="value"></span>

Notice that when you type something, the field loses its focus...

查看更多
女痞
3楼-- · 2020-05-03 10:44

i think it is issue with type of input field, decimal point (the dot) is not a number but it is necessary for numbers, so i guess it is the glitch in the matrix.

here is a fiddle where type is text and it is working fine.

查看更多
登录 后发表回答