jQuery - 无法在Firefox中编辑/删除输入值 [英] jQuery - Can not edit/delete input value in Firefox

查看:59
本文介绍了jQuery - 无法在Firefox中编辑/删除输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限制用户输入最多两位小数的值,即 10.56

Restricting user to enter values upto two decimal i.e. 10.56 only.

<input class="number" type="text" value="" />
<input type="text" value="44" />



$('.number').on('keypress',function (event) {
    if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }
    var input = $(this).val();
    if ((input.indexOf('.') != -1) && (input.substring(input.indexOf('.')).length > 2)) {
        event.preventDefault();
    }
});

小提琴的作品。但是如果用户按 delte / tab / backspace ,则没有任何反应。

The fiddle works. But if a user press delte/tab/backspace, nothing happens.

如何允许用户编辑/删除输入值或使用标签按钮移动到下一个输入框?

How to allow user to edit/delete input value or move to next input box using tab button ?

答案参考:链接

推荐答案

您可以转义相关的键盘键。请参阅 Javascript Char Codes ,以了解密钥转义。

You can escape the related keyboards keys. See the Javascript Char Codes, to know the key escaped.

查看现场演示:

$('.number').on('keypress',function (event) {

  // Add this condition to escape arrow/tab/delete/... and many others keys
  if(event.which <= 46) {
    return true;
  }

  if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
    event.preventDefault();
  }
  var input = $(this).val();
  if ((input.indexOf('.') != -1) && (input.substring(input.indexOf('.')).length > 2)) {
    event.preventDefault();
  }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="number" type="text" value="" />

<input type="text" value="44" />

更新:

添加你的小提琴如果有问题:

Add in your fiddle an if confition :

var acceptedKeys = [9, 37, 39, 46, 8];
if(acceptedKeys.indexOf(charCode) > -1) {
    return true;
} 

使用此代码,您接受以下密钥:

With this code you accept this keys :


  • 退格

  • 删除

  • 左箭头

  • 右箭头

  • 标签

  • backspace
  • delete
  • left arrow
  • right arrow
  • tab

http://jsfiddle.net/e0orb0qq/29/

这篇关于jQuery - 无法在Firefox中编辑/删除输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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